SQL Server中的implicit_transactions

一 spid 60
set implicit_transactions on
select @@TRANCOUNT--值为0
update testtable set c1=11 where id=1
select @@TRANCOUNT--值为1,
select * from testtable with(readpast)--且id为1的记录已成功修改为11,但没有commit则仍锁定该记录,但在同一spid可查询到已被修改
select @@TRANCOUNT--值为1
commit --提交修改
select @@TRANCOUNT--提交后值为1
rollback --没有回退的transaction,报错"The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION."
select @@TRANCOUNT--值为0


二 spid 63
set implicit_transactions off
select @@TRANCOUNT--值为0
update testtable set c1=11 where id=1
select @@TRANCOUNT--值为0
select * from testtable--id为1的记录已成功修改为11
select @@TRANCOUNT--值为0
rollback--不能回退,报错"The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION."

三 总结
默认implicit_transactions 是 off。
启用隐含交易(set implicit_transactions on)模式后,
数据库引擎会当需要交易管理的第一句语句执行时候自动激活一笔新交易,从而必须显示地commit才能生效。

原文地址:https://www.cnblogs.com/dotagg/p/6364472.html