Trigger - Not Roger Lloyd Pack

Good Morning SQL Geeks!

I've set up a trigger on a local table to send an email when a record gets inserted. Works beautifully, except when it doesn't. I can't figure out why it would fire sometimes and not fire other times. All expected records are in the table, I set a flag when the email has been sent and it stays at 'N' when the email hasn't been sent.

Any ideas on why some inserts will fire the trigger and some won't? It's a very simple table and all the relevant information is in the correct fields.

Brain scratching time for a Friday.

Thanks all, Debbie

Parents
  • Former Member
    Former Member $organization

    Debbie

     

    My best guess would be that something in the @body_text query might be causing sp_send_dbmail to fail though not sending the email nor updating the table. You could try creating a simple table with a varchar field and insert the value of @body_text before trying to send the email and then check the values for the ones that fail.

     

    Fernando Margueirat
    Business Analyst
    The National Ballet of Canada
    470 Queens Quay West
    Toronto, Ontario
    M5V 3K4
    P: 416 345 9686 x453
    F: 416 345 8323

     

    From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Debbie Harland
    Sent: January-17-14 9:23 AM
    To: Fernando Margueirat
    Subject: Re: [Tessitura Technical Forum] Trigger - Not Roger Lloyd Pack

     

    Hi Fernando,

    Its very simple. The trigger looks like this:

     

     

     

     

     

     

     

     

     

     

     

     

    declare

     

    @id int, @check varchar(1), @body_text nvarchar(max), @create_date datetime

    select

     

    @id = id_key from inserted

    set

     

    @create_date = GETDATE()

    set

     

    @body_text =(SELECT 'Customer: ' + convert(varchar(10),customer_no) + ' ' + name + ', with Attribute: ' + attribute + ', purchased tickets for Performance: ' + performance + ' on ' + CONVERT(varchar(10), perf_date, 103) + '. Purchased on: ' + convert(varchar(10), order_date, 103) from lt_ovt_report_check where id_key = @id and (email_sent is null or email_sent = ' ' or email_sent = 'N'))

    BEGIN

     

    EXEC msdb.dbo.sp_send_dbmail

     

     

     @recipients = 'debbie.harland@oldvictheatre.com',

    @profile_name

    = 'OVIC',

    @subject

    = 'Illuminate Ticket Purchase',

    @body

    = @body_text

     

    UPDATE LT_OVT_REPORT_CHECK set email_sent = 'Y' where id_key = @id

    END

    I've got a scheduled task which checks once an hour for tickets bought by Customers with a specific attribute. If it finds one, it does an insert into my local table with all the relevant information. The trigger then picks up that information and puts it into an email.

    The trigger has worked today, but didn't fire a couple of times yesterday. The record was inserted into the local table with all the information, just no email sent and the 'email_sent' field was still set to 'N'.

    I think it might be something to do with the timing, but I've not figured it yet.

    Thanks for looking.

    Debbie

    From: Fernando Margueirat <bounce-fernandomargueirat4910@tessituranetwork.com>
    Sent: 1/17/2014 6:57:17 AM

    Debbie

    If you share your code we can give it a look and see what could be causing the problem.

    Fernando




    This message was sent automatically to you by www.tessituranetwork.com because you subscribed to the Tessitura Technical Forum. You may reply to this message to post to the Technical forum or visit the site to search, read and post to the forums. In the interest of keeping the forum posts from becoming cluttered, we encourage you to delete previous message text from your reply before sending. Thank you!

Reply
  • Former Member
    Former Member $organization

    Debbie

     

    My best guess would be that something in the @body_text query might be causing sp_send_dbmail to fail though not sending the email nor updating the table. You could try creating a simple table with a varchar field and insert the value of @body_text before trying to send the email and then check the values for the ones that fail.

     

    Fernando Margueirat
    Business Analyst
    The National Ballet of Canada
    470 Queens Quay West
    Toronto, Ontario
    M5V 3K4
    P: 416 345 9686 x453
    F: 416 345 8323

     

    From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Debbie Harland
    Sent: January-17-14 9:23 AM
    To: Fernando Margueirat
    Subject: Re: [Tessitura Technical Forum] Trigger - Not Roger Lloyd Pack

     

    Hi Fernando,

    Its very simple. The trigger looks like this:

     

     

     

     

     

     

     

     

     

     

     

     

    declare

     

    @id int, @check varchar(1), @body_text nvarchar(max), @create_date datetime

    select

     

    @id = id_key from inserted

    set

     

    @create_date = GETDATE()

    set

     

    @body_text =(SELECT 'Customer: ' + convert(varchar(10),customer_no) + ' ' + name + ', with Attribute: ' + attribute + ', purchased tickets for Performance: ' + performance + ' on ' + CONVERT(varchar(10), perf_date, 103) + '. Purchased on: ' + convert(varchar(10), order_date, 103) from lt_ovt_report_check where id_key = @id and (email_sent is null or email_sent = ' ' or email_sent = 'N'))

    BEGIN

     

    EXEC msdb.dbo.sp_send_dbmail

     

     

     @recipients = 'debbie.harland@oldvictheatre.com',

    @profile_name

    = 'OVIC',

    @subject

    = 'Illuminate Ticket Purchase',

    @body

    = @body_text

     

    UPDATE LT_OVT_REPORT_CHECK set email_sent = 'Y' where id_key = @id

    END

    I've got a scheduled task which checks once an hour for tickets bought by Customers with a specific attribute. If it finds one, it does an insert into my local table with all the relevant information. The trigger then picks up that information and puts it into an email.

    The trigger has worked today, but didn't fire a couple of times yesterday. The record was inserted into the local table with all the information, just no email sent and the 'email_sent' field was still set to 'N'.

    I think it might be something to do with the timing, but I've not figured it yet.

    Thanks for looking.

    Debbie

    From: Fernando Margueirat <bounce-fernandomargueirat4910@tessituranetwork.com>
    Sent: 1/17/2014 6:57:17 AM

    Debbie

    If you share your code we can give it a look and see what could be causing the problem.

    Fernando




    This message was sent automatically to you by www.tessituranetwork.com because you subscribed to the Tessitura Technical Forum. You may reply to this message to post to the Technical forum or visit the site to search, read and post to the forums. In the interest of keeping the forum posts from becoming cluttered, we encourage you to delete previous message text from your reply before sending. Thank you!

Children
  • Former Member
    Former Member $organization in reply to Former Member

    I'll second Fernando's suggestion.  I would also look at the records that failed and see if there are any commonalities.  Like any record with a particular attribute fail consistently.  Or everything for one performance fails.

    I can't decide if this would be a problem or not, but I'd check to see if some of your performances or attributes have special characters in them (in particular a single quote).  For instance, a performance title of "The O'Malley Chronicles" might be causing an error when sendmail tries to parse the string in @body_text.

  • Good suggestions. Yes, it would make sense if something was missing from one of the variables. I'll check it out.

    Thanks, Debbie