Solved

Sending email on trigger

Posted on 2011-03-17
6
431 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
ID: 35158578
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
ID: 35158608
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
ID: 35158906
Can someone help me with the syntax I would need to loop through the log table and send out email for each row?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35158978
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
ID: 35159574
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
ID: 35159968
Do you have an example of the temp table approach that I could use as a template for mine?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

828 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