Sql server 2005 在SQL Server中,我如何知道什么事务模式是I';我现在在用什么?

Sql server 2005 在SQL Server中,我如何知道什么事务模式是I';我现在在用什么?,sql-server-2005,tsql,transactions,Sql Server 2005,Tsql,Transactions,在SQL Server中,如何知道当前使用的是什么事务模式?例如自动提交、显式或隐式。如何使用tsql将一种模式更改为另一种模式? 非常感谢 select @@OPTIONS & 2 如果返回2,则处于隐式事务模式。如果它返回0,则表示您处于自动提交状态 要切换您所处的模式,请使用 set implicit_transactions on 或 我认为没有办法确定当前事务是显式启动还是隐式启动。因此,这段代码只是尝试猜测:如果隐式_事务处于关闭状态,则假定事务是显式启动的 MSDN

在SQL Server中,如何知道当前使用的是什么事务模式?例如自动提交、显式或隐式。如何使用tsql将一种模式更改为另一种模式? 非常感谢

select @@OPTIONS & 2
如果返回2,则处于隐式事务模式。如果它返回0,则表示您处于自动提交状态

要切换您所处的模式,请使用

set implicit_transactions on

我认为没有办法确定当前事务是显式启动还是隐式启动。因此,这段代码只是尝试猜测:如果隐式_事务处于关闭状态,则假定事务是显式启动的

MSDN参考资料:


对以前发布的脚本进行轻微修改-如果没有活动事务且隐式事务关闭,则连接处于自动提交模式:

IF @@TRANCOUNT = 0 AND (@@OPTIONS & 2 = 0)
  PRINT 'No current transaction, autocommit mode (default)'
ELSE IF @@TRANCOUNT = 0 AND (@@OPTIONS & 2 = 2)
  PRINT 'Implicit transactions is on, no transaction started yet'
ELSE IF @@OPTIONS & 2 = 0 
  PRINT 'Implicit transactions is off, explicit transaction is currently running'
ELSE 
  PRINT 'Implicit transactions is on, implicit or explicit transaction is currently running' + CAST(@@OPTIONS & 2 AS VARCHAR(5))

它应该是隐式的。@daniel\u不正确,现在已更改。不知道为什么以前没有发现它。打开/关闭此选项是在服务器级别还是在事务发生的活动会话中发生的?@SQLnbe-这是一个连接级别设置。请参阅:“当连接在隐式事务模式下运行时…”(我的emhpasis)我认为消息
无当前事务,自动提交模式(默认值)
有点误导,因为此时自动提交并不完全确定,可以为隐式事务设置连接,但因为尚未发出任何语句,尚未启动任何事务。
IF @@TRANCOUNT = 0 PRINT 'No current transaction, autocommit mode (default)'
ELSE IF @@OPTIONS & 2 = 0 PRINT 'Implicit transactions is off, explicit transaction is currently running'
ELSE PRINT 'Implicit transactions is on, implicit or explicit transaction is currently running'
IF @@TRANCOUNT = 0 AND (@@OPTIONS & 2 = 0)
  PRINT 'No current transaction, autocommit mode (default)'
ELSE IF @@TRANCOUNT = 0 AND (@@OPTIONS & 2 = 2)
  PRINT 'Implicit transactions is on, no transaction started yet'
ELSE IF @@OPTIONS & 2 = 0 
  PRINT 'Implicit transactions is off, explicit transaction is currently running'
ELSE 
  PRINT 'Implicit transactions is on, implicit or explicit transaction is currently running' + CAST(@@OPTIONS & 2 AS VARCHAR(5))