1
我正在使用SQL Server 2014;我的存儲過程將是嵌套的事務過程,從而它將調用少數存儲過程中有交易。如果內部存儲過程或者一個打一個錯誤,然後將回退所有,如SQL Server存儲過程中的嵌套事務
Begin Try
Begin Tran
Exec Stored Proc 1 (with begin tran inside)
Exec Stored Proc 2 (with begin tran inside)
Exec Stored Proc 3 (with begin tran inside)
Exec Stored Proc 4 (with begin tran inside)
Exec Stored Proc 5 (with begin tran inside)
Commit Tran
End Try
Begin Catch
Catch exception then roll back tran
End Catch
問題是事務計數後執行的內部存儲過程是不匹配的,但是如果我沒有打開交易在內部存儲過程中,它不會回滾。任何人都可以給我一些建議嗎?
在每個過程中使用try catch。在內部過程中回滾事務並將錯誤返回給外部過程。在外部過程中,你從任何內部過程中得到一個錯誤打破每個事務的調用循環,並回滾 – scsimon
hi @scsimon,謝謝你的建議。假設存儲過程1和2成功執行,但存儲過程3命中錯誤。那麼外部捕獲和回滾將回滾存儲過程1和2? –