Solved

Sending email on trigger

Posted on 2011-03-17
6
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

635 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