在SQL Server中,我如何知道我当前使用的事务模式?

时间:2010-05-27 06:42:50

标签: sql-server-2005 tsql transactions

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

3 个答案:

答案 0 :(得分:9)

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'

我认为无法确定当前事务是明确还是隐式启动。因此,此代码只是试图猜测:如果IMPLICIT_TRANSACTIONS为OFF,则假定事务是显式启动的。

MSDN参考:

答案 1 :(得分:5)

select @@OPTIONS & 2

如果返回2,则表示您处于隐式事务模式。如果它返回0,那么你就是自动提交。

BOL for @@OPTIONS

BOL for what each option is

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

set implicit_transactions on

set implicit_transactions off

答案 2 :(得分:4)

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

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))