嗨,我目前对Tsql中嵌套事务的理解是,如果您有多个事务(几个事务嵌套在一个“外部”事务中),那么所有的传递都必须被提交(对于任何一个事务,“外部”事务是最后一个事务)对数据库进行更改.如果提交的数量少于开放交易的数量,则不进行与任何交易相关的更改.这是一个关于嵌套交易如何工作的正确概述吗?
解决方法
您对COMMIT的描述是正确的.
Kalen Delaney has an article covering the same type of behavior that you describe.
然而,正如Kalen的文章所述,嵌套事务中的ROLLBACK将回滚整个外部事务,而不仅仅是回滚发生的内部事务.
请注意以下结果:
BEGIN TRAN SELECT @@trancount BEGIN TRAN SELECT @@trancount BEGIN TRAN SELECT @@trancount ROLLBACK TRAN SELECT @@trancount
这在MSDN文章Nesting Transactions中有描述:
A ROLLBACK WORK or a ROLLBACK TRANSACTION statement that does not have a transaction name rolls back all nested transactions and decrements @@TRANCOUNT to 0. A ROLLBACK TRANSACTION that uses the transaction name of the outermost transaction in a set of nested transactions rolls back all of the nested transactions and decrements @@TRANCOUNT to 0. When you are unsure if you are already in a transaction,SELECT @@TRANCOUNT to determine if it is 1 or more. If @@TRANCOUNT is 0,you are not in a transaction.