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?
imstac73Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
lluddenConnect With a Mentor Commented:
Don't include a long process like sending email in a trigger.

If you need to do something like this, then create a table and add entries to it with the trigger, and then create a job  that runs on a set schedule, checks the table for new entries, and sends off the emails.

The job can be run with SQL agent and scheduled to run every 10 min or so (or whatever you require).
0
 
Aaron ShiloChief Database ArchitectCommented:
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.
0
 
imstac73Author Commented:
Can someone help me with the syntax I would need to loop through the log table and send out email for each row?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Aaron ShiloChief Database ArchitectCommented:
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.
0
 
LowfatspreadCommented:
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...
0
 
imstac73Author Commented:
Do you have an example of the temp table approach that I could use as a template for mine?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.