嵌套事务及事务分类1

全局变量@@trancount

  返回当前连接的活动事务数

显示事务

  用begin transaction明确指定事务的开始

  最常用的事务类型

隐性事务

  通过设置set implicit_transactions on语句,将隐性事务模式设置为打开】

  其后的T_SQL语句自动启动一个新事物

  提交或回滚一个事务后,下一个T-SQL语句又将启动一个新事物

自动提交事务

SQL sever 的默认模式

每条单独的T-SQL语句视为一个事务

例子

--commit 对事务数的影响 (使用嵌套事务)
print @@trancount  --在没有事务的时候查看事务数

begin tran   --开始事务


    print @@trancount   --开始事务, @@trancount将被设置为1
    begin tran  --开始第二个事务

        print @@trancount     --事务数+1

    commit tran   --提交第二个事务

    print @@trancount   --事务数减一

commit tran   --提交第一个事务,事务数-1

print @@trancount

go

--rollback(回滚\撤销)对事务的影响
print @@trancount  --在没有事务的时候查看事务数

begin tran  --开始一个事务数
    print @@trancount  --事务数+1

    begin tran  --开始第二个事务

        print @@trancount  --事务数 +1
rollback tran  --回滚事务,将事务数清0,所有活动的事务都将回滚
print @@trancount
go

猜你喜欢

转载自www.cnblogs.com/zhangxudong-cnblogs/p/10878487.html