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

后端 未结 3 617
旧巷少年郎
旧巷少年郎 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:33

    It depends on the severity level of the error. Sufficiently high -- 16, perhaps? -- the process may stop at the failing line, leaving the transaction open and your locks in place. If there's any chance of error within a transaction, you definitely want to wrap it in the try-catch block, as you did in your first example.

提交回复
热议问题