sql server.  dml triggers  ddl trigers  insert triggers  delete triggers  update...

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

Post on 02-Jan-2016

214 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • SQL SERVER

  • DML TriggersDDL Trigers

  • INSERT triggersDELETE triggersUPDATE triggersA mix and match of any of the above

  • ALTER TRIGGER TriggerOne ON PersonAFTER UPDATE ASSELECT D.LastName + changed to + I.LastNameFROM Inserted AS IINNER JOIN Deleted AS DON I.PersonID = D.PersonID;GOUPDATE PersonSET LastName = CarterWHERE LastName = Johnson;Result:------------------------------------------Johnson changed to CarterJohnson 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 MessagesUpdating summary informationFeeding de-normalized tables for reportingSetting condition flags

  • Complex data validationWriting data-audit trailsMaintaining modified date columnsEnforcing custom referential-integrity checks and cascading deletes

  • CREATE TRIGGER Sales.SalesOrderDetailNotDiscontinuedON Sales.SalesOrderDetailFOR INSERT, UPDATE ASIF EXISTS(SELECT True FROM Inserted iJOIN Production.Product p ON i.ProductID = p.ProductIDWHERE p.DiscontinuedDate IS NOT NULL)BEGINRAISERROR(Order Item is discontinued. Transaction Failed.,16,1)ROLLBACK TRANEND

  • UPDATE Production.ProductSET DiscontinuedDate = 01-01-2008WHERE ProductID = 680

    INSERT INTO Sales.SalesOrderDetailVALUES(43659, 4911-403C-98, 1, 680, 1, 1431.50,0.00, NEWID(), GETDATE())

  • Msg 50000, Level 16, State 1, Procedure SalesOrderDetailNotDiscontinued, Line 14Order Item is discontinued. Transaction Failed.Msg 3609, Level 16, State 1, Line 1The 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.ProductIsRationedON Production.ProductInventoryFOR UPDATE AS IF EXISTS(SELECT True FROM Inserted iJOIN Deleted d ON i.ProductID = d.ProductIDAND i.LocationID = d.LocationIDWHERE (d.Quantity - i.Quantity) > d.Quantity / 2AND d.Quantity i.Quantity > 0)BEGINRAISERROR(Cannot reduce stock by more than 50%% at once.,16,1)ROLLBACK TRANEND

  • ALTER TABLE TRIGGER

  • sp_settriggerorder[@triggername =] ,[@order =] {FIRST|LAST|NONE},[@stmttype =] {INSERT|UPDATE|DELETE}[, [@namespace =] {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 SweetDont Forget Triggers When Choosing IndexesTry Not to Roll Back Within Triggers

  • DROP TRIGGER [.]

  • CREATE, ALTER, or DROP databaseRESTORE database or log

  • ALTER TRIGGER Production.ProductIsRationedON Production.ProductInventoryFOR UPDATE ASIF UPDATE(Quantity)BEGINIF EXISTS(SELECT TrueFROM Inserted iJOIN Deleted dON i.ProductID = d.ProductIDAND i.LocationID = d.LocationIDWHERE (d.Quantity - i.Quantity) > d.Quantity / 2AND d.Quantity > 0 )BEGINRAISERROR(Cannot reduce stock by more than 50%% at once.,16,1)ROLLBACK TRANENDEND

  • | Represents bitwise OR& Represents bitwise AND^ Represents bitwise Exclusive OR

Recommended

View more >