Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger with RAISERROR Expand / Collapse
Author
Message
Posted Monday, September 30, 2013 5:37 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 8:30 AM
Points: 576, Visits: 1,292
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





If you need to work better, try working less...
Post #1499938
Posted Monday, September 30, 2013 6:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:49 AM
Points: 1,144, Visits: 3,431
You need to use ROLLBACK inside the trigger.

CREATE TRIGGER trg_test_upd ON test AFTER UPDATE
AS
RAISERROR('Error', 16, 1);
ROLLBACK;
GO





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1499961
Posted Monday, September 30, 2013 6:37 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:49 AM
Points: 1,144, Visits: 3,431
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





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1499962
Posted Monday, September 30, 2013 7:39 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Yesterday @ 8:30 AM
Points: 576, Visits: 1,292
Thanks,

Thought that the XACT_ABORT should solve it but it doesn't.




If you need to work better, try working less...
Post #1500007
Posted Wednesday, October 2, 2013 5:38 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 4, 2016 7:09 AM
Points: 252, Visits: 436
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.
Post #1500730
Posted Tuesday, September 13, 2016 6:05 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 13, 2016 7:06 AM
Points: 1, Visits: 7
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.
Post #1817229
Posted Tuesday, September 13, 2016 6:16 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 4:50 PM
Points: 45,339, Visits: 43,574
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1817231
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse