Trigger defined for Insert, Update

  • Is there an "easy/quick" way to determine inside the trigger itself whether it is being fired because of an Insert or because of an Update when a trigger has been defined for Insert, Update?

    Without inquiring whether a row exists or not, is there a system variable that can determine this?

  • If your trigger is For Update, Insert

    there is not any system information identifying what action caused the trigger to fire.

    You could duplicate your code, and create 1 trigger for insert, and 1 trigger for update, or test for Existing rows as you mentioned.

  • You could do something like:

    DECLARE @operation as Varchar(10);

    DECLARE @Count as int;

    SET @operation = 'Inserted';

    SELECT @Count = COUNT(*) FROM DELETED;

    if @Count > 0

    BEGIN

    SET @operation = 'Deleted';

    SELECT @Count = COUNT(*) FROM INSERTED;

    IF @Count > 0

    SET @operation = 'Updated'

    END

    HTH,

    Rob

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

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