简体   繁体   中英

Error handling in stored procedures

I have a single "Pre-Processing" Stored procedure which calls multiple stored procedures inside.

Finally when all my SPS (inside) have successfully executed, I want to run an update statment, so in the main SP I have :

EXEC SP1
EXEC SP2
EXEC SP3

-- RUN UPDATE statment here

All of my inner SPs have the following template :

  BEGIN TRY
  BEGIN TRANSACTION

  // DO SOME INSERT,UPDATE ETC..

  COMMIT TRANSACTION;
  END TRY
  BEGIN CATCH
         SELECT 
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;

  IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
         RETURN
  END CATCH

My question is as to what is the best approach for error handling in this scenario, ie I want to make sure everything got done before I run that final update statement.

由于我发现每个SP必须具有自己的错误处理,而外部SP调用多个SP则不需要进行错误处理,因此结束此操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM