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

SE APLICA A: síSQL Server síAzure SQL Database síAzure Synapse Analytics (SQL DW) síAlmacenamiento de datos paralelos APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

Devuelve el número de instrucciones BEGIN TRANSACTION que se han producido en la conexión actual.Returns the number of BEGIN TRANSACTION statements that have occurred on the current connection.

Icono de vínculo de tema Convenciones de sintaxis de Transact-SQLTopic link icon Transact-SQL Syntax Conventions

SintaxisSyntax

@@TRANCOUNT  

Tipos devueltosReturn Types

integerinteger

NotasRemarks

La instrucción BEGIN TRANSACTION incrementa @@TRANCOUNT en 1.The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION disminuye @@TRANCOUNT a 0, salvo para ROLLBACK TRANSACTION savepoint_name, que no afecta a @@TRANCOUNT.ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT. Cada instrucción COMMIT TRANSACTION o COMMIT WORK disminuye @@TRANCOUNT en uno.COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1.

EjemplosExamples

A.A. Mostrar los efectos de las instrucciones BEGIN y COMMITShowing the effects of the BEGIN and COMMIT statements

En el ejemplo siguiente se muestra el efecto que tienen las instrucciones anidadas BEGIN y COMMIT en la variable @@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. Mostrar los efectos de las instrucciones BEGIN y ROLLBACKShowing the effects of the BEGIN and ROLLBACK statements

En el ejemplo siguiente se muestra el efecto que tienen las instrucciones anidadas BEGIN TRAN y ROLLBACK en la variable @@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  

Consulte tambiénSee 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)
Funciones del sistema (Transact-SQL)System Functions (Transact-SQL)