--- title: "@@TRANCOUNT (Transact-SQL) | Microsoft Docs" ms.custom: "" ms.date: "08/29/2017" ms.prod: "sql-non-specified" ms.reviewer: "" ms.suite: "" ms.technology: - "database-engine" ms.tgt_pltfrm: "" ms.topic: "language-reference" f1_keywords: - "@@TRANCOUNT_TSQL" - "@@TRANCOUNT" dev_langs: - "TSQL" helpviewer_keywords: - "@@TRANCOUNT function" - "number of active transactions" - "connections [SQL Server], active transactions" - "active transactions" ms.assetid: b2638410-e410-4bd0-9b54-90096182b2b6 caps.latest.revision: 40 author: "BYHAM" ms.author: "rickbyh" manager: "jhubbard" --- # @@TRANCOUNT (Transact-SQL) [!INCLUDE[tsql-appliesto-ss2008-all_md](../../includes/tsql-appliesto-ss2008-all-md.md)] Returns the number of BEGIN TRANSACTION statements that have occurred on the current connection. ![Topic link icon](../../database-engine/configure-windows/media/topic-link.gif "Topic link icon") [Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md) ## Syntax ``` @@TRANCOUNT ``` ## Return Types **integer** ## Remarks The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION *savepoint_name*, which does not affect @@TRANCOUNT. COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1. ## Examples ### A. Showing the effects of the BEGIN and COMMIT statements 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. Showing the effects of the BEGIN and ROLLBACK statements 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 ``` ## See Also [BEGIN TRANSACTION (Transact-SQL)](../../t-sql/language-elements/begin-transaction-transact-sql.md) [COMMIT TRANSACTION (Transact-SQL)](../../t-sql/language-elements/commit-transaction-transact-sql.md) [ROLLBACK TRANSACTION (Transact-SQL)](../../t-sql/language-elements/rollback-transaction-transact-sql.md) [System Functions (Transact-SQL)](../../relational-databases/system-functions/system-functions-for-transact-sql.md)