DML and DDL Triggers
This lesson explains AFTER and INSTEAD OF triggers, the inserted and deleted tables, DDL triggers, and when simpler features should replace triggers.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish between AFTER and INSTEAD OF triggers.
- Explain why trigger code must be set-based.
- Identify alternatives to triggers for common requirements.
AFTER and INSTEAD OF triggers
An AFTER trigger runs after the INSERT, UPDATE or DELETE and its constraint checks succeed, inside the same transaction, so it can enforce rules across tables, write audit rows or roll back with THROW. An INSTEAD OF trigger replaces the statement, which is how a view over several tables accepts modifications, or how an application's delete becomes a soft delete. A table can have many AFTER triggers per action but only one INSTEAD OF trigger per action.
AFTER or INSTEAD OF?
The inserted and deleted tables
A trigger fires once per statement, however many rows it affects, so trigger code must be set-based. The inserted pseudo-table holds new row versions (for INSERT and UPDATE) and deleted holds old versions (for DELETE and UPDATE); an UPDATE has both. UPDATE(column) or COLUMNS_UPDATED() tells whether a column appeared in the SET clause, not whether its value changed, so comparing inserted with deleted finds real changes.
CREATE OR ALTER TRIGGER Catalog.trg_Product_PriceAudit
ON Catalog.Product
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF NOT UPDATE(ListPrice) RETURN;
INSERT INTO Catalog.PriceChange (ProductID, OldPrice, NewPrice, ChangedUtc, ChangedBy)
SELECT i.ProductID, d.ListPrice, i.ListPrice, SYSUTCDATETIME(), ORIGINAL_LOGIN()
FROM inserted AS i
JOIN deleted AS d ON d.ProductID = i.ProductID
WHERE i.ListPrice <> d.ListPrice;
END;DDL triggers and design cautions
A DDL trigger fires for schema events at database scope (such as CREATE_TABLE, ALTER_PROCEDURE or the DDL_DATABASE_LEVEL_EVENTS group) or server scope on SQL Server. EVENTDATA() returns XML with the event type, object and T-SQL command, which can be logged, and ROLLBACK blocks the change.
Triggers add hidden work to every write and run inside the caller's transaction, so a slow trigger slows and blocks the application, and an error in it rolls back the user's statement. Triggers should never call external services, such as a model endpoint, synchronously; they should record the work in a queue table for a background process. Simpler alternatives often exist, as the following table shows.
| Requirement | Simpler alternative to a trigger |
|---|---|
| Row-level validation | CHECK constraint |
| Default values | DEFAULT constraint |
| Full row history | Temporal table |
| Referential integrity and cascades | FOREIGN KEY with referential actions |
| Notify other systems of changes | Change Tracking, CDC or change event streaming |
Simpler than a trigger
A trigger that fails on bulk updates
An AFTER UPDATE trigger stores SELECT @id = ProductID FROM inserted in a variable and writes one audit row. When a price update changes 500 products in one statement, only one audit row appears. How should the developer fix the trigger?
Rewrite the trigger to insert from a join of inserted and deleted, so it writes one audit row for every changed product in one set-based statement. Triggers fire once per statement, and assigning from inserted to a variable keeps only one arbitrary row. A cursor over inserted would also work but is slower, and setting the trigger to fire per row is not possible in SQL Server.
Does UPDATE(Price) inside a trigger prove that the price value changed?
Show answer
No. It shows that Price was in the SET clause; comparing inserted with deleted shows real changes.
Exam Tips
Remember for the exam
- Triggers fire once per statement; write set-based code over inserted and deleted.
- Updatable multi-table views and soft deletes: INSTEAD OF triggers.
- UPDATE(column) means the column was in SET, not that its value changed.
- Never call external services synchronously from a trigger; queue the work.