March 3, 2016 at 12:31 pm
Hi,
I was wondering if anyone had some advice or example for creating a trigger that fires when a specific table is updated. Every time an entry is added to this table, I would like this trigger to fire.
March 3, 2016 at 1:27 pm
CREATE TRIGGER reminder2
ON Sales.Customer
AFTER INSERT, UPDATE, DELETE
AS
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks2012 Administrator',
@recipients = 'danw@Adventure-Works.com',
@body = 'Don''t forget to print a report for the sales force.',
@subject = 'Reminder';
GO
March 3, 2016 at 1:41 pm
Don't know what you want your trigger to do but be careful. Triggers fire once per operation, not once per row. So you need to create set based queries in your triggers. And stay away from sending emails directly from your trigger like the previous example does. You do NOT want your inserts to wait on that. If you want to send emails it is better to put the data in a holding table and have another process pick up the information for emails on a schedule.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 3, 2016 at 2:10 pm
What I need to do is send an email every time an entry is added to a specific table. Any ideas or suggestion on how best to this are appreciated.
March 3, 2016 at 2:12 pm
Sean Lange (3/3/2016)
Don't know what you want your trigger to do but be careful. Triggers fire once per operation, not once per row. So you need to create set based queries in your triggers. And stay away from sending emails directly from your trigger like the previous example does. You do NOT want your inserts to wait on that. If you want to send emails it is better to put the data in a holding table and have another process pick up the information for emails on a schedule.
sp_send_dbmail would not cause a wait, right? it's already an asynchronous service broker.
as long as permissions to send the mail are taken care of(so the trigger doesn't fail with the error user does not have permission to sp_send_dbmail) I'm under the impression , sending an email in a trigger would not cause a delay.
the old database mail from 2000 days, (whatever that was, I'm blocking it out the memory willfully now), yes, that was synchronous and evil.
Lowell
March 3, 2016 at 2:17 pm
Lowell (3/3/2016)
Sean Lange (3/3/2016)
Don't know what you want your trigger to do but be careful. Triggers fire once per operation, not once per row. So you need to create set based queries in your triggers. And stay away from sending emails directly from your trigger like the previous example does. You do NOT want your inserts to wait on that. If you want to send emails it is better to put the data in a holding table and have another process pick up the information for emails on a schedule.sp_send_dbmail would not cause a wait, right? it's already an asynchronous service broker.
as long as permissions to send the mail are taken care of(so the trigger doesn't fail with the error user does not have permission to sp_send_dbmail) I'm under the impression , sending an email in a trigger would not cause a delay.
the old database mail from 2000 days, (whatever that was, I'm blocking it out the memory willfully now), yes, that was synchronous and evil.
You are probably right Lowell. I don't make a habit of sending emails from triggers. I think if I were doing it I would do it that way just to be sure but it is probably overkill with an asynchronous broker. Thanks for the correction.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 3, 2016 at 3:11 pm
Now if I want to grab information from this new row that has been entered, what is the best way before sending the email?
CREATE TRIGGER reminder2
ON Sales.Customer
AFTER INSERT, UPDATE, DELETE
AS
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks2012 Administrator',
@recipients = 'danw@Adventure-Works.com',
@body = 'Don''t forget to print a report for the sales force.',
@subject = 'Reminder';
GO
March 3, 2016 at 3:16 pm
TJT (3/3/2016)
Now if I want to grab information from this new row that has been entered, what is the best way before sending the email?CREATE TRIGGER reminder2
ON Sales.Customer
AFTER INSERT, UPDATE, DELETE
AS
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'AdventureWorks2012 Administrator',
@recipients = 'danw@Adventure-Works.com',
@body = 'Don''t forget to print a report for the sales force.',
@subject = 'Reminder';
GO
You missed the point. You will need to look at the inserted or deleted virtual tables to retrieve those values. And then you MUST use a loop. This is why I suggest using a holding table. You can have your insert in the trigger be set based, including the values. Then have another process that uses a cursor to iterate RBAR over the holding table.
And since you say you want values for new rows you don't want to have your trigger fire for updates or deletes right?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 3, 2016 at 3:51 pm
Hi SSCoach, do you have an example of a holding table? Also do you have an example of retrieving data from the " inserted or deleted virtual tables"?
March 3, 2016 at 6:09 pm
TJT (3/3/2016)
Hi SSCoach, do you have an example of a holding table? Also do you have an example of retrieving data from the " inserted or deleted virtual tables"?
You cannot support such a thing with the knowledge you currently don't have. You really need to study up on triggers before you even think of using them. Please see the following URL for a start. I recommend you read it all and try things on a test bed before you write production code.
https://msdn.microsoft.com/en-us/library/ms189799.aspx
After that, I strongly recommend that you do a search on "triggers in SQL Server" using Google and learn some more. Triggers are not to be handled in a trivial manner. They will eat your face off if you don't program them correctly.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 4, 2016 at 9:29 am
I'd also recommend reading this [/url], since I wrote it.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
March 4, 2016 at 9:54 am
And an excellent article for getting started with triggers. A must read!!
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply