Pregunta

Just say I have a throw or RaiseError in a trigger like below

IF OBJECT_ID('Sales.OrderDetails_AfterTrigger', 'TR') IS NOT NULL
DROP Trigger Sales.OrderDetails_AfterTrigger;
GO
CREATE TRIGGER Sales.OrderDetails_AfterTrigger ON Sales.OrderDetails
AFTER INSERT, UPDATE
AS
BEGIN
IF @@ROWCOUNT = 0 RETURN;
SET NOCOUNT ON;
-- Check all rows
IF EXISTS(...)
BEGIN
RAISERROR  ('This error message is not displayed', 10, 1 ) 
END
END
GO

If the raiseerror is thrown in the trigger will that prevent the dml statement from being rolled back?

Just that I notice I am still getting rows inserted when this happens.

¿Fue útil?

Solución

check out the remarks section in the raise error page from microsoft http://msdn.microsoft.com/en-us/library/ms178592.aspx

"When RAISERROR is run with a severity of 11 or higher in a TRY block, it transfers control to the associated CATCH block"

Your throwing a 10 so control will NOT go to the catch statement. That'll help with your calling code flow if you are trying to stop it there (not shown so hard to tell).

But!! you made a comment that rows are still inserted. I suspect you should perhaps be using a insert update trigger not an "after" trigger.

Since you throw a 10 your rows are already inserted and it won't break a transaction.

Sorry can't be more precise but kind of hard without knowing what context you are calling the insert/update statement triggering the trigger

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top