@ (Transact-SQL)@@TRANCOUNT (Transact-SQL)

适用对象: yesSQL ServeryesAzure SQL 数据库yesAzure SQL 数据仓库yes并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

返回在当前连接上执行的 BEGIN TRANSACTION 语句的数目。Returns the number of BEGIN TRANSACTION statements that have occurred on the current connection.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

@@TRANCOUNT  

返回类型Return Types

integerinteger

RemarksRemarks

BEGIN TRANSACTION 语句将 @@TRANCOUNT 增加 1。The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION 将 @@TRANCOUNT 递减到 0,但 ROLLBACK TRANSACTION savepoint_name 除外,它不影响 @@TRANCOUNT 。ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT. COMMIT TRANSACTION 或 COMMIT WORK 将 @@TRANCOUNT 递减 1。COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1.

示例Examples

A.A. 演示 BEGIN 和 COMMIT 语句的效果Showing the effects of the BEGIN and COMMIT statements

下面的示例演示嵌套的 BEGINCOMMIT 语句对 @@TRANCOUNT 变量产生的效果。The following example shows the effect that nested BEGIN and COMMIT statements have on the @@TRANCOUNT variable.

PRINT @@TRANCOUNT  
--  The BEGIN TRAN statement will increment the  
--  transaction count by 1.  
BEGIN TRAN  
    PRINT @@TRANCOUNT  
    BEGIN TRAN  
        PRINT @@TRANCOUNT  
--  The COMMIT statement will decrement the transaction count by 1.  
    COMMIT  
    PRINT @@TRANCOUNT  
COMMIT  
PRINT @@TRANCOUNT  
--Results  
--0  
--1  
--2  
--1  
--0  

B.B. 演示 BEGIN 和 ROLLBACK 语句的效果Showing the effects of the BEGIN and ROLLBACK statements

下面的示例演示嵌套的 BEGIN TRANROLLBACK 语句对 @@TRANCOUNT 变量产生的效果。The following example shows the effect that nested BEGIN TRAN and ROLLBACK statements have on the @@TRANCOUNT variable.

PRINT @@TRANCOUNT  
--  The BEGIN TRAN statement will increment the  
--  transaction count by 1.  
BEGIN TRAN  
    PRINT @@TRANCOUNT  
    BEGIN TRAN  
        PRINT @@TRANCOUNT  
--  The ROLLBACK statement will clear the @@TRANCOUNT variable  
--  to 0 because all active transactions will be rolled back.  
ROLLBACK  
PRINT @@TRANCOUNT  
--Results  
--0  
--1  
--2  
--0  

另请参阅See Also

BEGIN TRANSACTION (Transact-SQL) BEGIN TRANSACTION (Transact-SQL)
COMMIT TRANSACTION (Transact-SQL) COMMIT TRANSACTION (Transact-SQL)
ROLLBACK TRANSACTION (Transact-SQL) ROLLBACK TRANSACTION (Transact-SQL)
系统函数 (Transact-SQL)System Functions (Transact-SQL)