Forum Discussion
Trigger to send email if a table has had a INSERT, UPDATE OR DELETE
We need a trigger to be sent out if ProcessSchedule has had an INERT, UPDATE or DELETE
We don't need details just a heads up type of email.
The send email part is easy.
EXEC msdb.dbo.sp_send_dbmail profile_name = 'SQLMail'
,@recipients = 'email address removed for privacy reasons'
,@body = 'Press Schedule Changed'
,@subject = 'Press Schedule Changed'
The trigger part I have not done before.
Any help would be greatly appreciated.
4 Replies
- rodgerkongIron Contributor
use SQL Script below to create a trigger:
CREATE TRIGGER SendNotifyMail ON ProcessSchedule AFTER INSERT, UPDATE, DELETE AS EXEC msdb.dbo.sp_send_dbmail profile_name = 'SQLMail' ,@recipients = 'email address removed for privacy reasons' ,@body = 'Press Schedule Changed' ,@subject = 'Press Schedule Changed' ; GO
And check out document https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-ver16 for more information- RuthlessRothCopper ContributorThanks. The trigger didn't work for this application. The issue is the table has a lot of Inserts, Updates and Deletes that do not relate to specific records. I resorted to a SQL Job to check every 30 minutes if a specific record or records have been modified within the past 30 minutes based on the data put into a UpdateDateTim field that is used when a record has been changed.
- rodgerkongIron Contributor
I used to add a rowversion column in table to identify which row had been modified. Its value will be auto increased with any update of the row. Check document https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?view=sql-server-ver16.