?
Solved

trigger will not allow inserted record to be selected for email

Posted on 2006-05-17
14
Medium Priority
?
319 Views
Last Modified: 2012-05-05
Here is the code:  There are nine emails created which is correct but I cannot select the inserted co number.  Also I want pick up the to and from email from the new_orders_email record.

CREATE TRIGGER notify ON dbo.new_orders_email_hdr FOR INSERT
AS

if @@ROWCOUNT = 0 return

DECLARE @co_number nVARCHAR(15)

declare @c cursor
set @c = cursor for select co_number from inserted order by co_number
open @c
fetch next from @c into @co_number
while @@FETCH_STATUS = 0
     BEGIN
       EXECUTE master.dbo.xp_sendmail 'ccaruso@stridetool.com',
      @query = 'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email',
      @no_header= 'FALSE',
      @width = 132,
      @dbuse='cognos',
      @subject= 'AutoNotice: Recent Orders'
         
               fetch next from @c into @co_number
     END

close @c
deallocate @c
0
Comment
Question by:cc22
  • 7
  • 7
14 Comments
 
LVL 3

Expert Comment

by:trainsdse
ID: 16702757
Do the new_orders_email_hdr and new_orders_email tables get inserted during the same transaction?
0
 

Author Comment

by:cc22
ID: 16702792
No just the new_orders_email_hdr.  The new_orders_email table already exists.
0
 
LVL 3

Expert Comment

by:trainsdse
ID: 16702898
So you are saying the emails are being generated, just their content is missing the co_number?
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:cc22
ID: 16703016
Yes.  But they contain all co_numbers not just the co being inserted.  I have tried to select co_number = @co_number but then no emails are generated.
0
 
LVL 3

Expert Comment

by:trainsdse
ID: 16704020
ok, gotcha, so your query that currently askes for everything in the whole table:

'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email'

needs to just ask for what was in this insert?  as in....

'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email WHERE co_number=' +  @co_number

On the right track?
0
 

Author Comment

by:cc22
ID: 16708080
Yes.   When I used your select statement I received incorrect syntax near +.

Here is the code.

CREATE TRIGGER notify ON dbo.new_orders_email_hdr FOR INSERT
AS

if @@ROWCOUNT = 0 return

DECLARE @co_number nVARCHAR(15)

declare @c cursor
set @c = cursor for select co_number from inserted order by co_number
open @c
fetch next from @c into @co_number
while @@FETCH_STATUS = 0
     BEGIN
       EXECUTE master.dbo.xp_sendmail 'ccaruso@stridetool.com',
      @query = 'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email WHERE co_number=' +  @co_number
      @no_header= 'FALSE',
      @width = 132,
      @dbuse='cognos',
      @subject= 'AutoNotice: Recent Orders'
         
               fetch next from @c into @co_number
     END

close @c
deallocate @c


0
 
LVL 3

Expert Comment

by:trainsdse
ID: 16708338
Oh, @co_number is a varchar, sorry, try it as

@query = 'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email WHERE co_number=''' +  @co_number + ''''
0
 

Author Comment

by:cc22
ID: 16708439
I received the same error.  Here is the code.  I've included the insert also.


CREATE TRIGGER notify ON dbo.new_orders_email_hdr FOR INSERT
AS

if @@ROWCOUNT = 0 return

DECLARE @co_number nVARCHAR(15)

declare @c cursor
set @c = cursor for select co_number from inserted order by co_number
open @c
fetch next from @c into @co_number
while @@FETCH_STATUS = 0
     BEGIN
       EXECUTE master.dbo.xp_sendmail 'ccaruso@stridetool.com',
      @query = 'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email WHERE co_number=''' +  @co_number + '''',
      @no_header= 'FALSE',
      @width = 132,
      @dbuse='cognos',
      @subject= 'AutoNotice: Recent Orders'
         
               fetch next from @c into @co_number
     END

close @c
deallocate @c


------------------------------------------------------------------------

INSERT
INTO new_orders_email_hdr
                      (CO_NUMBER,
CUST_ID, CUST_NAME, CSR, fromname, fromaddress, subject, HTS)
SELECT     CO_NUMBER, CUST_ID, CUST_NAME, CSR, fromname, fromaddress,
subject, HTS
FROM         new_orders_email
GROUP BY HTS, CO_NUMBER,
CUST_NAME, CSR, CUST_ID, fromname, fromaddress, subject
ORDER
BY CO_NUMBER
0
 
LVL 3

Expert Comment

by:trainsdse
ID: 16709043
Let's try assigning the query to a variable first, try this:

  BEGIN
       Declare @q varchar(8000)
       SET @q = 'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email WHERE co_number='
       SET @q = @q + ''''
       SET @q = @q + @co_number
       SET @q = @q + ''''
       EXECUTE master.dbo.xp_sendmail 'ccaruso@stridetool.com',
      @query = @q,
      @no_header= 'FALSE',
      @width = 132,
      @dbuse='cognos',
      @subject= 'AutoNotice: Recent Orders'
         
               fetch next from @c into @co_number
     END

If that also causes an error, change the @query param to @message and let's see what sql it is creating by looking at the query in the email sent.

Thomas
0
 

Author Comment

by:cc22
ID: 16710279
Thank you!

The code worked. I still need to be able to use the toemail and fromaddress fields in the new_orders_email instead of hardcoding my email address.
0
 
LVL 3

Expert Comment

by:trainsdse
ID: 16710463
Ok, to get the To Address you can use the following (Just change whateverTheToFieldIsCalled to what it should be):

 BEGIN
       Declare @q varchar(8000)
       SET @q = 'select co_number, co_ln_no, cust_id, cust_name, csr, item, order_qty  from new_orders_email WHERE co_number='
       SET @q = @q + ''''
       SET @q = @q + @co_number
       SET @q = @q + ''''

      Declare @toAddy varchar(300)
      SET @toAddy = (SELECT top 1 whateverTheToFieldIsCalled from new_orders_email where co_number=@co_number)
       EXECUTE master.dbo.xp_sendmail @toAddy,
      @query = @q,
      @no_header= 'FALSE',
      @width = 132,
      @dbuse='cognos',
      @subject= 'AutoNotice: Recent Orders'
         
               fetch next from @c into @co_number
     END

For the from, you can use a @set_user parameter, but the value for that param should be a windows account like domain\user
If you don't have that info or if the sender is not on the domain - or if sql doesn't have a login for those individuals, you may be out of luck on the from.  We typically use the generic sql account for our froms.

Thomas
0
 

Author Comment

by:cc22
ID: 16710692
The to worked great.  I will experiment with the from.  I was wondering if you had any tips on formatting.  I want to put the cust_name, co_number and cust_id in the subject.    
0
 
LVL 3

Accepted Solution

by:
trainsdse earned 1000 total points
ID: 16711169
Just get those like I got the to: declare a variable and set it to the result from the subquery, then concatenate the subject line together however you like.

HTH,

Thomas
0
 

Author Comment

by:cc22
ID: 16711210
Thanks,

I tried using the setuser but I am using windows authentication so I doesn't work.  I do have users set up that I tried but it does not work.  If you have any other ideas let me know.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question