Recognazie Trigger Action

  • Hi

    I have created a trigger for all insert,update and delete actions like this

    Create trigger tg_name on tb_name

    after insert,update and delete

    as

    .

    .

    .

    How I can recognize that which action has fired the trigger?

    Thanks

  • Hi,

    For this you need to make assumptions as:

    IF EXISTS(SELECT * FROM INSERTED)

    AND NOT EXISTS(SELECT * FROM DELETED) BEGIN

    -- this is an insert trigger

    END

    IF EXISTS(SELECT * FROM INSERTED)

    AND EXISTS(SELECT * FROM DELETED) BEGIN

    -- this is an update trigger

    END

    IF NOT EXISTS(SELECT * FROM INSERTED)

    AND EXISTS(SELECT * FROM DELETED) BEGIN

    -- this is a delete trigger

    END

    Let me know if it helps you.

    Thanks,

    Tejas Shah

    http://www.sqlyoga.com

  • you can also have a flag field on the target table with a default value of 0 and update it to 1 from within your trigger statements.

    this way u'll be able to identify by query the table for all rows that were modified by the trigger operation.



    Pradeep Singh

  • Dera Tejas Shah

    Thank you for your reply,Your solution is very good and practical.This is exactly what I need

    With many thanks to other replies

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

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