imstac73
asked on
Sending email on trigger
I want to send an email any time a new record or the status changes on a record on one of my tables (Jobs).
I was going to set up a trigger on the table to call the sendmail stored procedure and send out the email; however I just read that it may be better to set the trigger to insert a record in a 'log' table and then have a job pick up new data and send the e-mail or blocking DML on the table and using a stored procedure to perform the update and the log.
This is my first time working with sending mail from SQL and triggers. Can someone give me advice on the best way to accomplish my end result?
I was going to set up a trigger on the table to call the sendmail stored procedure and send out the email; however I just read that it may be better to set the trigger to insert a record in a 'log' table and then have a job pick up new data and send the e-mail or blocking DML on the table and using a stored procedure to perform the update and the log.
This is my first time working with sending mail from SQL and triggers. Can someone give me advice on the best way to accomplish my end result?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can someone help me with the syntax I would need to loop through the log table and send out email for each row?
if were talking about a fairly small amount of rows consider using a CURSOR
read this : http://msdn.microsoft.com/en-us/library/ms180169.aspx
if its a large amount of rows then just use a temp table and a while loop
to loop thru the results and send mail for each row.
read this : http://msdn.microsoft.com/en-us/library/ms180169.aspx
if its a large amount of rows then just use a temp table and a while loop
to loop thru the results and send mail for each row.
i'd favour the temp table approach all the time... that should enale you to avoid most contention issues with both the job and the system wanting to manipulate the log table simultaneously...
ASKER
Do you have an example of the temp table approach that I could use as a template for mine?
the second way would be better.
insert into a log table the relevant data and then send the mail.
this will reduce wait adn lock time on the table itself.