You only need one trigger
CREATE TRIGGER [ProductAfter] ON [Product] AFTER INSERT, UPDATE, DELETE
You can determine which DML statement triggers the trigger based on the number of entries in the inserted and deleted tables available in the body of the trigger. For INSERT , deleted empty, for DELETE , inserted empty, for UPDATE both inserted and deleted not empty. For example,
IF @@ROWCOUNT = 0 -- exit trigger when zero records affected BEGIN RETURN; END; DECLARE @type CHAR(1);-- 'U' for update, 'D' for delete, 'I' for insert IF EXISTS(SELECT * FROM inserted) BEGIN IF EXISTS(SELECT * FROM deleted) BEGIN SET @type ='U'; END ELSE BEGIN SET @type ='I'; END END ELSE BEGIN SET @type = 'D'; END;
In addition, see Tracking data changes , there is another option for tracking changes without triggers.
a1ex07
source share