Link to home
Start Free TrialLog in
Avatar of imstac73
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?
Avatar of Aaron Shilo
Aaron Shilo
Flag of Israel image

hi

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.
ASKER CERTIFIED SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of imstac73
imstac73

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.
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...
Do you have an example of the temp table approach that I could use as a template for mine?