@@TRANCOUNT (Transact-SQL)

Retorna o número de instruções BEGIN TRANSACTION que ocorreram na conexão atual.

Aplica-se a: SQL Server (SQL Server 2008 até a versão atual), Banco de dados SQL do Windows Azure (versão inicial até a versão atual).

Ícone de vínculo de tópico Convenções da sintaxe Transact-SQL

Sintaxe

@@TRANCOUNT

Tipos de retorno

integer

Comentários

A instrução BEGIN TRANSACTION incrementa @@TRANCOUNT em 1. ROLLBACK TRANSACTION diminui @@TRANCOUNT para 0, com exceção de ROLLBACK TRANSACTION savepoint_name, que não afeta @@TRANCOUNT. COMMIT TRANSACTION ou COMMIT WORK diminui @@TRANCOUNT em 1.

Exemplos

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

O exemplo a seguir mostra o efeito das instruções aninhadas BEGIN e COMMIT sobre a variável @@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.Mostrando os efeitos das instruções BEGIN e ROLLBACK

O exemplo a seguir mostra o efeito das instruções aninhadas BEGIN TRAN e ROLLBACK sobre a variável @@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 também

Referência

BEGIN TRANSACTION (Transact-SQL)

COMMIT TRANSACTION (Transact-SQL)

ROLLBACK TRANSACTION (Transact-SQL)

Funções de sistema (Transact-SQL)