Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: Today @ 2:34 AM
Points: 513, Visits: 1,132
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


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 1:48 AM
Points: 907, Visits: 2,869
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


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 1:48 AM
Points: 907, Visits: 2,869
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: Today @ 2:34 AM
Points: 513, Visits: 1,132
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-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 18, 2014 8:34 AM
Points: 135, Visits: 250
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
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse