@@TRANCOUNT (Transact-SQL)

Se aplica a:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalytics Platform System (PDW)Almacenamiento en Microsoft Fabric

Devuelve el número de instrucciones BEGIN TRANSACTION que se han producido en la conexión actual.

Convenciones de sintaxis de Transact-SQL

Sintaxis

@@TRANCOUNT  

Nota:

El grupo de SQL sin servidor no admite esta sintaxis en Azure Synapse Analytics.

Nota:

Para ver la sintaxis de Transact-SQL para SQL Server 2014 (12.x) y versiones anteriores, consulte Versiones anteriores de la documentación.

Tipos de valor devuelto

integer

Comentarios

La instrucción BEGIN TRANSACTION incrementa @@TRANCOUNT en 1. ROLLBACK TRANSACTION disminuye @@ TRANCOUNT a 0, salvo para ROLLBACK TRANSACTION nombre_de_punto_de_retorno, que no afecta a @@TRANCOUNT. Cada instrucción COMMIT TRANSACTION o COMMIT WORK disminuye @@TRANCOUNT en uno.

Ejemplos

A. Mostrar los efectos de las instrucciones BEGIN y COMMIT

En el ejemplo siguiente se muestra el efecto que tienen las instrucciones anidadas BEGIN y COMMIT en la variable @@TRANCOUNT.

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. Mostrar los efectos de las instrucciones BEGIN y ROLLBACK

En el ejemplo siguiente se muestra el efecto que tienen las instrucciones anidadas BEGIN TRAN y ROLLBACK en la variable @@TRANCOUNT.

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én

BEGIN TRANSACTION (Transact-SQL)
COMMIT TRANSACTION (Transact-SQL)
ROLLBACK TRANSACTION (Transact-SQL)
Funciones del sistema (Transact-SQL)