September 30, 2013 at 5:37 am
Hi,
Why doesn't a RAISERROR inside an AFTER trigger "break" the transaction? Is there any flag or SET option to do it?
I created a table with a trigger that just has RAISERROR and the UPDATE is executed any way...
CREATE TABLE test (ID INT NOT NULL, Val INT);
GO
INSERT INTO test VALUES (1,1), (2,2), (3,3);
GO
CREATE TRIGGER trg_test_upd ON test AFTER UPDATE
AS
RAISERROR('Error', 16, 1)
GO
If I execute:
SELECT * FROM test
BEGIN TRAN
UPDATE test SET val = 3 WHERE id = 1
COMMIT TRAN
SELECT * FROM test
The update is committed even with the error raised from the trigger.
The only way it isn't committed is if I have error handling:
SELECT * FROM test
BEGIN TRAN
BEGIN TRY
UPDATE test SET val = 3 WHERE id = 1
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
END CATCH
SELECT * FROM test
September 30, 2013 at 6:35 am
September 30, 2013 at 6:37 am
The behavior is the same with a stored proc
CREATE TABLE test (ID INT NOT NULL, Val INT);
GO
INSERT INTO test VALUES (1,1), (2,2), (3,3);
GO
CREATE PROCEDURE trg_test_upd
AS
UPDATE test SET val = 3 WHERE id = 1
RAISERROR('Error', 16, 1);
GO
SELECT * FROM test
BEGIN TRAN
EXEC trg_test_upd
COMMIT TRAN
SELECT * FROM test
September 30, 2013 at 7:39 am
Thanks,
Thought that the XACT_ABORT should solve it but it doesn't.
October 2, 2013 at 5:38 am
PiMané (9/30/2013)
Hi,Why doesn't a RAISERROR inside an AFTER trigger "break" the transaction? Is there any flag or SET option to do it?
The update is committed even with the error raised from the trigger.
The only way it isn't committed is if I have error handling:
RAISERROR simply tells SQL Server where to transfer control or send a message based on where it is in your code and the severity level of the error. From BOL:
When RAISERROR is run with a severity of 11 or higher in a TRY block, it transfers control to the associated CATCH block. The error is returned to the caller if RAISERROR is run:
Outside the scope of any TRY block.
With a severity of 10 or lower in a TRY block.
With a severity of 20 or higher that terminates the database connection.
RAISERROR doesn't "break" a transaction simply because you may not need to do so. You may encounter issues that have nothing to do with data integrity where you just want to return something to the caller as a warning (say, an inventory level is approaching some minimum value) yet let the transaction commit normally. BOL indicates RAISERROR can be used as a substitute for the print statement because you can use C-style printf character substitution, which print won't do.
There's probably more nuances than that (I'll let the real pros comment on that) but I believe that is the essential nugget.
____________
Just my $0.02 from over here in the cheap seats of the peanut gallery - please adjust for inflation and/or your local currency.
September 13, 2016 at 6:05 am
Perhaps better to rollback before raiserror.
You don't know what an error handler would do, especially time wise.
Releasing a transaction at the earliest will release locks, thus, wait at other connections.
September 13, 2016 at 6:16 am
Please note: 3 year old thread.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply