2010-05-27 68 views

回答

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

我认为消息'没有当前事务,自动提交模式(默认)'有点误导,因为在这一点上自动提交并不完全确定,可以为隐式事务设置连接,但由于没有语句尚未发布交易。 – MaxiWheat 2017-01-11 21:17:49

5
select @@OPTIONS & 2 

如果返回2,则表示处于隐式事务模式。如果它返回0,则表示自动提交。

BOL for @@OPTIONS

BOL for what each option is

要切换你在哪种模式,你会使用

set implicit_transactions on 

set implicit_transactions off 
+0

它应该是implicit_transactions。 – 2013-10-18 13:42:55

+0

@daniel_aren - 真的,现在改变了。不知道为什么它没有被发现之前。 – 2013-10-18 13:44:47

+0

开启/关闭是否发生在服务器级别或事务正在进行的活动会话? – SQLnbe 2014-10-30 00:41:08

3

轻微修改以前发布的脚本 - 连接是在自动提交模式下,如果没有活跃的交易,隐性交易关闭:

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)) 
相关问题