Trigger

Code that runs automatically when a table's rows change.

What is Trigger?

A trigger is code that runs automatically when rows are inserted, updated or deleted in a table, for example to write an audit log. Use them sparingly: they are easy to forget about.

CREATE TRIGGER dbo.Recipes_Audit ON dbo.Recipes AFTER UPDATE AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO dbo.AuditLog (Message, CreatedAt)
    SELECT CONCAT(N'Recipe changed: ', Title), SYSUTCDATETIME() FROM inserted;
END

Level: Journeyman · More Database terms at this level

Updated