Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL SERVER.  DML Triggers  DDL Trigers  INSERT triggers  DELETE triggers  UPDATE triggers  A mix and match of any of the above.

Similar presentations


Presentation on theme: "SQL SERVER.  DML Triggers  DDL Trigers  INSERT triggers  DELETE triggers  UPDATE triggers  A mix and match of any of the above."— Presentation transcript:

1 SQL SERVER

2  DML Triggers  DDL Trigers

3  INSERT triggers  DELETE triggers  UPDATE triggers  A mix and match of any of the above

4

5

6  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)

7

8  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

9  Complex data validation  Writing data-audit trails  Maintaining modified date columns  Enforcing custom referential-integrity checks and cascading deletes

10  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

11  UPDATE Production.Product  SET DiscontinuedDate = ‘01-01-2008’  WHERE ProductID = 680  INSERT INTO Sales.SalesOrderDetail  VALUES  (43659, ‘4911-403C-98’, 1, 680, 1, 1431.50,0.00, NEWID(), GETDATE())

12  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.

13  CREATE TRIGGER  ON [.]  [WITH ENCRYPTION | EXECUTE AS >]  {{{FOR|AFTER} } |INSTEAD OF}  [WITH APPEND]  [NOT FOR REPLICATION]  AS  | EXTERNAL NAME >

14  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

15  ALTER TABLE  TRIGGER >

16

17  sp_settriggerorder[@triggername =] ‘ ’,  [@order =] ‘{FIRST|LAST|NONE}’,  [@stmttype =] ‘{INSERT|UPDATE|DELETE}’  [, [@namespace =] {‘DATABASE’ | ‘SERVER’ | NULL} ]

18  Controlling Firing Order for Logic Reasons  Controlling Firing Order for Performance Reasons

19 EXEC sp_configure ‘Nested Triggers’, 0; RECONFIGURE;

20  ALTER DATABASE DatabaseName SET RECURSIVE_TRIGGERS ON | OFF ;

21  Keep It Short and Sweet  Don’t Forget Triggers When Choosing Indexes  Try Not to Roll Back Within Triggers

22  DROP TRIGGER [.]

23  CREATE, ALTER, or DROP database  RESTORE database or log

24  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

25

26

27  | Represents bitwise OR  & Represents bitwise AND  ^ Represents bitwise Exclusive OR


Download ppt "SQL SERVER.  DML Triggers  DDL Trigers  INSERT triggers  DELETE triggers  UPDATE triggers  A mix and match of any of the above."

Similar presentations


Ads by Google