SQL - Best way to determine Operation Type in Trigger?
sql, sql-server-2012, t-sql, triggers
Solution
One: yes. Two: you can be even more concise than that.
Here is the code I am using currently.
SELECT @Action = CASE
WHEN EXISTS(SELECT 1 FROM INSERTED)
AND EXISTS(SELECT 1 FROM DELETED) THEN 'U'
WHEN EXISTS(SELECT 1 FROM INSERTED) THEN 'I'
ELSE 'D' END;
Problem
Below is the code i'm using to determine if the operation in an insert/update/delete. This question is two part. One - this is a correct way of determining the operation type. Two - Is this the best way of determining the operation type. ``` BEGIN DECLARE @ActionType CHAR (1) IF NOT EXISTS (SELECT * FROM deleted) AND EXISTS (SELECT * FROM inserted) BEGIN SET @ActionType = 'I' END IF EXISTS (SELECT * FROM deleted) AND EXISTS (SELECT * FROM inserted) BEGIN SET @ActionType = 'U' END IF EXISTS (SELECT * FROM deleted) AND NOT EXISTS (SELECT * FROM inserted) BEGIN SET @ActionType = 'D' END Select @ActionType; End ```