osmansays,
Combine a trigger and a stored procedure to send a email on updates
--------------------------------
Procedure like :
Create Procedure sp_SMTPMail
@.SenderName varchar(100),
@.SenderAddress varchar(100),
@.RecipientName varchar(100),
@.RecipientAddress varchar(100),
@.Subject varchar(200),
@.Body varchar(8000),
@.MailServer varchar(100) = 'localhost'
AS
SET nocount on
declare @.oMail int
declare @.resultcode int
EXEC @.resultcode = sp_OACreate 'SMTPsvg.Mailer', @.oMail OUT
if @.resultcode = 0
BEGIN
EXEC @.resultcode = sp_OASetProperty @.oMail, 'RemoteHost', @.mailserver
EXEC @.resultcode = sp_OASetProperty @.oMail, 'FromName', @.SenderName
EXEC @.resultcode = sp_OASetProperty @.oMail, 'FromAddress', @.SenderAddress
EXEC @.resultcode = sp_OAMethod @.oMail, 'AddRecipient', NULL, @.RecipientName, @.RecipientAddress
EXEC @.resultcode = sp_OASetProperty @.oMail, 'Subject', @.Subject
EXEC @.resultcode = sp_OASetProperty @.oMail, 'BodyText', @.Body
EXEC @.resultcode = sp_OAMethod @.oMail, 'SendMail', NULL
EXEC sp_OADestroy @.oMail
END
SET nocount off
--------------------------------
Trigger like :
CREATE TRIGGER trgDataChanged on tblData
AFTER UPDATE
AS
BEGIN
exec sp_SMTPMail @.SenderName='me', @.SenderAddress='me@.somewhere.com', @.RecipientName = 'Someone', @.RecipientAddress = 'someone@.someplace.com', @.Subject='SQL Data Change', @.body='data in table tblData has been changed'
END
--------------------------------
If you also want to track the changes you can either translate the query to simple data or show the data that is changed but to view that you need to walk through the recordset with for instance a cursor.
Peter
Friday, March 23, 2012
How can I be notified when record is updated
I want to build an windows application by using a visual C# to Notify the user that his data in the database had been changed ..such like "New Message In Your Mail Box Alert"..So I need to know if there is way that to let the SQL Server send a notify (just like Trigger) ..
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment