Terminate procedure if any called procedure fails

  • This shouldn't be hard, but TRY/CATCH and RAISEERROR are just enough different from other programming languages that I'm not a bit comfortable with them.

    I have simple cover procedures that only execute a series of specific procedures, e.g.:

    exec proc1

    exec proc2

    exec proc3

    What is the accepted way to simply exit the cover procedure on the failure of any of the specific procedures?

  • You can use 2 different ways to exit the procedure.

    1. Implement a try catch with a raiserror (If i remember well, you need to raise an error with a severity of over 11, or something, but you can read this in BOL)

    2. Or, launch the procedure from the main procedure like this:

    Create proc mainProc1

    as

    BEGIN

    exec PROC1

    if @@Error > 0

    BEGIN

    Insert into ErrorTable 'Proc 1 has failed'

    Return

    END

    END

    But, the second way will not give you a trace of the error, unless you implement something around it. Depends on what you need to do, trace the error in a table, or raise the error to a client application.

    Cheers,

    J-F

  • Do you need to account for data integrity also if any of the SP's fail? Then look at SET XACT_ABORT (Transact-SQL) This will ensure that if any SP fails then the entire transaction is rolled back.

    [font="Courier New"]Sankar Reddy | http://SankarReddy.com/[/url][/font]

  • Thanks to you both! That should get me started.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply