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? –