Error handling in Stored procedures

  • Hi,

    i need to do some error handling in my stored proc i plan to use @@Error and if its not equal to 0 roll back transaction and return error code as out put parameter,if that a correct way to do deal with??

    thnx in advance

    sudheer.






    Regards,
    Sudheer 

    My Blog

  • Yes. I would imagine so.

    Just a reminder, when using @@ERROR, note that it gives the error of most recent sql statement. Hence it is better to store this @@ERROR in a variable and use the variable to check for conditions, return as output parameter etc.,

  • Is there an "On Error Resume Next" command? I am creating these procedures that bomb out when they encounter an error and I need to do some clean up if they do encounter an error.

  • No but you can use goto to branch, eg

    declare @ErrorNo int

    (sql statement)

    SET @ErrorNo = @@ERROR

    IF @ErrorNo <> 0 GOTO Error_handler

    (sql statement)

    SET @ErrorNo = @@ERROR

    IF @ErrorNo <> 0 GOTO Error_handler

    RETURN

    Error_handler:

    (clean up)

    Far away is close at hand in the images of elsewhere.
    Anon.

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

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