• 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.