Solved

Sending email on trigger

Posted on 2011-03-17
6
428 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:imstac73
6 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
Comment Utility
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
 
LVL 18

Accepted Solution

by:
lludden earned 250 total points
Comment Utility
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
 

Author Comment

by:imstac73
Comment Utility
Can someone help me with the syntax I would need to loop through the log table and send out email for each row?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:Aaron Shilo
Comment Utility
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
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
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
 

Author Comment

by:imstac73
Comment Utility
Do you have an example of the temp table approach that I could use as a template for mine?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now