?
Solved

Send an Email on Record Update

Posted on 2009-05-05
8
Medium Priority
?
335 Views
Last Modified: 2012-08-13
hi,
I want to know what is the best way of sending an email notifications from a database when a record is updated?
I have been reading a lot of posts on how we are not suppose to send an email from a trigger.
I am using SQL Server 2008 and I have a table called Project and a table called Employee, I want to be able to send an email to selected users in the Employee table (their emails are stored in EmailAddress Field, and only "Admins" Employees with their AccessLevel is set to 2) when the Project.Status field changes to 0 or 1 in the Project table

how can I do this?
Thanks
0
Comment
Question by:smaguire
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 21

Expert Comment

by:JestersGrind
ID: 24307192
If you're using a stored procedure to update the Project.Status field, you could change it to also send an email.

Greg


0
 

Author Comment

by:smaguire
ID: 24307269
No, I have a web application that enables the enduser to update any project. no stored procedure in the process.
I have it working using visual studio - C# code, but I want the code to be more dynamic where I don't have to compile the entire code everytime i need to subscribe a user to the email notification alert.
 
Thanks
0
 
LVL 21

Expert Comment

by:JestersGrind
ID: 24308736
In that case, a trigger might be your best option.

Greg


0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:smaguire
ID: 24308802
Thanks Greg for your reply,
I just finished reading some posts suggesting that NS is no longer in SQL Server 2008, and Triggers will not be recommended since if they do fail to contact the mail server, the commit will take a long time.
Now back to your original suggestion of using a SP, How can I accomplish that?
Thanks
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24312130
you could do something like below script. script is just example, you can make it more customize for your need.




create proc updateANDemail
as
begin
 
update table1 set field1='abc' where field1='ab'
 
EXEC master.dbo.xp_sendmail 
    @recipients=N'ritesh_a_shah@yahoo.com',
     @message=N'records updated',
     @copy_recipients=N'rits4friends@gmail.com',
     @subject=N'update' ;
 
 
end

Open in new window

0
 

Author Comment

by:smaguire
ID: 24314640
Thanks RiteshShah for your reply,
I will have to try your code later on but will I be able to do something like this:
@message=field1            
What is the 'N' for?
Thanks
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 1500 total points
ID: 24314738
you can remove N, its a prefix before string variable.... as long as field1 concern, than you would have to do it like:

create proc updateANDemail
as
begin

declare @val='ABC'

update table1 set field1=@val where field1='ab'
 
EXEC master.dbo.xp_sendmail
    @recipients='ritesh_a_shah@yahoo.com',
     @message=@val,
     @copy_recipients='rits4friends@gmail.com',
     @subject='update' ;
 end
0
 

Author Closing Comment

by:smaguire
ID: 31578127
Thanks
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

752 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