@@TRANCOUNT (Transact-SQL)

Retourne le nombre de transactions actives de la connexion actuelle.

Icône Lien de rubriqueConventions de la syntaxe de Transact-SQL

Syntaxe

@@TRANCOUNT

Types de retour

integer

Notes

L'instruction BEGIN TRANSACTION incrémente la valeur de @@TRANCOUNT de 1. ROLLBACK TRANSACTION décrémente @@TRANCOUNT à 0, à l'exception de ROLLBACK TRANSACTION savepoint_name, qui n'affecte pas @@TRANCOUNT. COMMIT TRANSACTION ou COMMIT WORK décrémente @@TRANCOUNT de 1.

Exemples

A. Illustration des effets des instructions BEGIN et COMMIT

L'exemple suivant illustre l'effet des instructions BEGIN et COMMIT imbriqués sur 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. Illustration des effets des instructions BEGIN et ROLLBACK

L'exemple suivant illustre l'effet des instructions BEGIN TRAN et ROLLBACK imbriqués sur 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