Trigger to send email if a table has had a INSERT, UPDATE OR DELETE

Copper Contributor

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

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-v... for more information

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

@RuthlessRoth 

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

Good Idea. Downside I do not have control over the table structure. I am working with a application that I did not design. When updates are sent out to the software it checks table structure and resets it to their design.