It is somewhat how you hope. If the transaction does not commit then an AFTER UPDATE
trigger will not fire, but it's only true for the entire transaction if you check the value of @@Error
after your statements and rollback the transaction.
Consider this:
CREATE TABLE Table1 (SomeNumber int NOT NULL)
CREATE TABLE Table2 (SomeNumber int)
CREATE TRIGGER UpdAfterTrigger
ON Table1
AFTER UPDATE
AS
BEGIN
INSERT Table2 SELECT 1
END
INSERT Table1 (SomeNumber) SELECT 1
BEGIN TRANSACTION
UPDATE Table1 SET SomeNumber=1
UPDATE Table1 SET SomeNumber=1
UPDATE Table1 SET SomeNumber=1
UPDATE Table1 SET SomeNumber=1
UPDATE Table1 SET SomeNumber=NULL
IF (@@error <> 0)
BEGIN
ROLLBACK TRANSACTION
END
ELSE
BEGIN
COMMIT TRANSACTION
END
The trigger will not fire in this case since the transaction is getting rolled back because of the error when inserting NULL
into a NOT NULL
column.
If you don't check for @@Error <> 0
and just blindly commit the transaction then the trigger in this example will fire for each of the 4 good UPDATE
statements in the batch.