SQL SERVER
DML Triggers DDL Trigers
INSERT triggers DELETE triggers UPDATE triggers A mix and match of any of the above
ALTER TRIGGER TriggerOne ON Person AFTER UPDATE AS SELECT D.LastName + ‘ changed to ’ + I.LastName FROM Inserted AS I INNER JOIN Deleted AS D ON I.PersonID = D.PersonID; GO UPDATE Person SET LastName = ‘Carter’ WHERE LastName = ‘Johnson’; Result: Johnson changed to Carter (2 row(s) affected)
Your business rule needs to reference data in a separate table. Your business rule needs to check the delta (difference between before and after) of an UPDATE. You require a customized error message. Using Triggers for Custom Error Messages Updating summary information Feeding de-normalized tables for reporting Setting condition flags
Complex data validation Writing data-audit trails Maintaining modified date columns Enforcing custom referential-integrity checks and cascading deletes
CREATE TRIGGER Sales.SalesOrderDetailNotDiscontinued ON Sales.SalesOrderDetail FOR INSERT, UPDATE AS IF EXISTS( SELECT ‘True’ FROM Inserted i JOIN Production.Product p ON i.ProductID = p.ProductID WHERE p.DiscontinuedDate IS NOT NULL) BEGIN RAISERROR(‘Order Item is discontinued. Transaction Failed.’,16,1) ROLLBACK TRAN END
UPDATE Production.Product SET DiscontinuedDate = ‘ ’ WHERE ProductID = 680 INSERT INTO Sales.SalesOrderDetail VALUES (43659, ‘ C-98’, 1, 680, 1, ,0.00, NEWID(), GETDATE())
Msg 50000, Level 16, State 1, Procedure SalesOrderDetailNotDiscontinued, Line 14 Order Item is discontinued. Transaction Failed. Msg 3609, Level 16, State 1, Line 1 The transaction ended in the trigger. The batch has been aborted.
CREATE TRIGGER ON [.] [WITH ENCRYPTION | EXECUTE AS >] {{{FOR|AFTER} } |INSTEAD OF} [WITH APPEND] [NOT FOR REPLICATION] AS | EXTERNAL NAME >
CREATE TRIGGER Production.ProductIsRationed ON Production.ProductInventory FOR UPDATE AS IF EXISTS( SELECT ‘True’ FROM Inserted i JOIN Deleted d ON i.ProductID = d.ProductID AND i.LocationID = d.LocationID WHERE (d.Quantity - i.Quantity) > d.Quantity / 2 AND d.Quantity – i.Quantity > 0) BEGIN RAISERROR(‘Cannot reduce stock by more than 50% at once.’,16,1) ROLLBACK TRAN END
ALTER TABLE TRIGGER >
=] ‘ ’, =] ‘{FIRST|LAST|NONE}’, =] ‘{INSERT|UPDATE|DELETE}’ [, =] {‘DATABASE’ | ‘SERVER’ | NULL} ]
Controlling Firing Order for Logic Reasons Controlling Firing Order for Performance Reasons
EXEC sp_configure ‘Nested Triggers’, 0; RECONFIGURE;
ALTER DATABASE DatabaseName SET RECURSIVE_TRIGGERS ON | OFF ;
Keep It Short and Sweet Don’t Forget Triggers When Choosing Indexes Try Not to Roll Back Within Triggers
DROP TRIGGER [.]
CREATE, ALTER, or DROP database RESTORE database or log
ALTER TRIGGER Production.ProductIsRationed ON Production.ProductInventory FOR UPDATE AS IF UPDATE(Quantity) BEGIN IF EXISTS( SELECT ‘True’ FROM Inserted i JOIN Deleted d ON i.ProductID = d.ProductID AND i.LocationID = d.LocationID WHERE (d.Quantity - i.Quantity) > d.Quantity / 2 AND d.Quantity > 0 ) BEGIN RAISERROR(‘Cannot reduce stock by more than 50% at once.’,16,1) ROLLBACK TRAN END
| Represents bitwise OR & Represents bitwise AND ^ Represents bitwise Exclusive OR