Link to home
Start Free TrialLog in
Avatar of smaguire
smaguireFlag for Canada

asked on

Send an Email on Record Update

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
Avatar of JestersGrind
JestersGrind
Flag of United States of America image

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

Greg


Avatar of smaguire

ASKER

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
In that case, a trigger might be your best option.

Greg


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
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

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
ASKER CERTIFIED SOLUTION
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks