Renaud is Not Bill Gates
Renaud is Not Bill Gates

Reputation: 2084

test if an action it was an update in triggers

I created a trigger for a table when a Delete/Update/Insert action has been made.

to test if it was a delete I use this statement :

if exists(select * from deleted)

and if it was an insert :

if exists(select * from inserted)

but how can I test if it was an update

Upvotes: 0

Views: 374

Answers (1)

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20775

 SET @action = 'I'; -- Set Action to Insert by default.
    IF EXISTS(SELECT * FROM DELETED)
    BEGIN
        SET @action = 
            CASE
                WHEN EXISTS(SELECT * FROM INSERTED) THEN 'U' -- Set Action to Updated.
                ELSE 'D' -- Set Action to Deleted.       
            END
    END
    ELSE 
    BEGIN
        IF NOT EXISTS(SELECT * FROM INSERTED) -- New record inserted.
             SET @action = 'I';
    END

Upvotes: 1

Related Questions