Combining triggers

  • I have two triggers for a table one for insert and one for update. Is there are any way to combine these triggers to a single trigger.

    Thanks

    Biju.

     

  • Yes, when creating the trigger use FOR INSERT,UPDATE.  If different logic is dependant upon what type of statement is executed, INSERT vs UPDATE, use the below T-SQL to execute statement type specific logic.

    CREATE TRIGGER trTestTrigger ON tblTest

    FOR INSERT,UPDATE,DELETE

    AS

    DECLARE @iStmntType INT

    SET @iStmntType = 0  /* 1=Delete;2=insert;3=update */

    IF EXISTS(Select * from deleted) SET @iStmntType = @iStmntType + 1

    IF EXISTS(Select * from inserted) SET @iStmntType = @iStmntType + 2

    IF @iStmntType = 1

     -- Perform delete logic (More than one command use BEGIN ... END)

    IF @iStmntType = 2

     -- Perform insert logic (More than one command use BEGIN ... END)

    IF @iStmntType = 3

     -- Perform update logic (More than one command use BEGIN ... END)

    Also check into using IF UPATED(column_name) and COLUMNS_UPDATED Logic

    Jason

    -

  • Thank you very much Jason , I have tried it and its worked.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply