Partilhar via


@@TRANCOUNT (Transact-SQL)

Aplica-se a:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsSistema de Plataforma de Análise (PDW)Armazém no Microsoft FabricBase de dados SQL no Microsoft Fabric

Devolve o número de extratos BEGIN TRANSACTION que ocorreram na ligação atual.

Transact-SQL convenções de sintaxe

Sintaxe

@@TRANCOUNT  

Observação

Essa sintaxe não é suportada pelo pool SQL sem servidor no Azure Synapse Analytics.

Tipos de devolução

integer

Observações

A instrução BEGIN TRANSACTION incrementa @@TRANCOUNT em 1. A DECREMENTO DA TRANSAÇÃO DE ROLLBACK @@TRANCOUNT até 0, exceto para a TRANSAÇÃO ROLLBACK savepoint_name, que não afeta a @@TRANCOUNT. COMMIT TRANSACTION ou COMMIT WORK decrement @@TRANCOUNT por 1.

Examples

A. Mostrar os efeitos das instruções BEGIN e COMMIT

O exemplo seguinte mostra o efeito que as instruções aninhadas BEGINCOMMIT e têm na @@TRANCOUNT variável.

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. Mostrando os efeitos das instruções BEGIN e ROLLBACK

O exemplo seguinte mostra o efeito que as instruções aninhadas BEGIN TRANROLLBACK e têm na @@TRANCOUNT variável.

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  

Ver também

INICIAR TRANSAÇÃO (Transact-SQL)
TRANSAÇÃO COMMIT (Transact-SQL)
TRANSAÇÃO DE REVERSÃO (Transact-SQL)
Funções do sistema (Transact-SQL)