Do I need to use a try..catch block, and explicit rollback in a SQL Server procedure?

后端 未结 3 615
旧巷少年郎
旧巷少年郎 2021-02-05 10:04

If I am coding a SQL Server (2008r2) procedure, and I wrap it in a transaction, do I need to explicitly enclose it in a try..catch block, and then explicitly call rollback in th

3条回答
  •  囚心锁ツ
    2021-02-05 10:36

    rollbacks will occur automatically if there is an error IN MOST CASES BUT NOT ALL

    if you want to guarantee a rollback for all errors precede the begin transaction with SET XACT_ABORT ON

    Best practice is to explicity catch errors with a try-catch block and take action there, including perhaps a rollback and reporting/logging the error.

提交回复
热议问题