SQL Server CURRENT_TRANSACTION_ID Function Guide
In this article
SQL Server's CURRENT_TRANSACTION_ID() returns the transaction ID of the current transaction, or NULL if no transaction is active. It is a built-in scalar function available since SQL Server 2016 (13.x) and is most useful for logging, auditing, and debugging: you can stamp every row written by a procedure with the same transaction ID, then group or filter by it later. No special setup is required beyond a supported SQL Server version and permission to run the query.
What You'll Need
- SQL Server 2016 or later (including Azure SQL Database and Azure SQL Managed Instance), or a compatible version. The function does not exist in SQL Server 2014 or earlier.
- A client such as SQL Server Management Studio (SSMS), Azure Data Studio, or any tool that can run T-SQL.
- Permission to execute the query in the target database; no special permission is needed for the function itself.
- An understanding that the function returns a
bigintvalue that is unique within the database, not across the server.
Step-by-Step
- Open a query window. Connect to your SQL Server instance in SSMS or Azure Data Studio and open a new query window against the database you want to test.
- Run the function outside a transaction. Execute the following to see the baseline behavior:
When no transaction is active, the result is NULL. This is expected and is the most common first surprise.SELECT CURRENT_TRANSACTION_ID() AS TransactionID; - Start an explicit transaction. Begin a transaction so the function has something to report:
You should now see a numeric value. Keep the transaction open for the next steps.BEGIN TRANSACTION; SELECT CURRENT_TRANSACTION_ID() AS TransactionID; - Confirm the value stays stable inside the transaction. Run the same SELECT again without committing. The value should be identical, because it identifies the current transaction, not each statement.
- Use it to tag rows. A practical pattern is to add a column to a log table and populate it with the transaction ID:
Because all inserts in the same transaction share the ID, you can later group byCREATE TABLE dbo.ChangeLog ( LogID int IDENTITY PRIMARY KEY, TransactionID bigint NULL, Message nvarchar(200), LoggedAt datetime2 DEFAULT SYSUTCDATETIME() ); BEGIN TRANSACTION; INSERT dbo.ChangeLog (TransactionID, Message) VALUES (CURRENT_TRANSACTION_ID(), 'Started order update'); -- ... other statements ... COMMIT TRANSACTION;TransactionIDto reconstruct what happened together. - Test nested transactions. SQL Server does not truly nest transactions;
BEGIN TRANSACTIONinside an existing transaction increments@@TRANCOUNTbut the transaction ID remains the same. Run:
Both SELECTs return the same value, which is the behavior you want for logging.BEGIN TRANSACTION; SELECT CURRENT_TRANSACTION_ID() AS OuterID; BEGIN TRANSACTION; SELECT CURRENT_TRANSACTION_ID() AS InnerID; ROLLBACK TRANSACTION; COMMIT TRANSACTION; - Check behavior after commit. After
COMMIT, run the SELECT again. It returns NULL because no transaction is active. This helps you avoid accidentally grouping unrelated statements. - Use it in a stored procedure. Inside a procedure, capture the value into a variable at the start and reuse it:
This avoids calling the function repeatedly and makes the intent clear.DECLARE @TranID bigint = CURRENT_TRANSACTION_ID(); INSERT dbo.ChangeLog (TransactionID, Message) VALUES (@TranID, 'Procedure started'); - Compare with other identifiers. Understand that
CURRENT_TRANSACTION_ID()is not the same as@@SPID(session ID) ortransaction_idinsys.dm_tran_current_transaction. The function is the simplest way to get the current transaction's ID without querying DMVs. - Clean up. If you left a transaction open during testing, issue
ROLLBACK TRANSACTION;orCOMMIT TRANSACTION;so you do not hold locks.
Common Mistakes
- Expecting a value outside a transaction. The function returns NULL when no transaction is active; it does not start one for you.
- Assuming the ID is global. Transaction IDs are unique within a database, not across the whole server or across databases. Do not use them as a cross-database key.
- Confusing it with
@@TRANCOUNT.@@TRANCOUNTcounts nesting levels;CURRENT_TRANSACTION_ID()identifies the transaction itself. - Using it in a computed column or indexed view. The function is nondeterministic and not allowed in those contexts.
- Forgetting that implicit transactions change behavior. If
IMPLICIT_TRANSACTIONSis ON, a transaction may already be active, so the function returns a value even without an explicitBEGIN TRANSACTION.
Tips
- Store the value in a
bigintcolumn; the function returnsbigint. - Capture it once per procedure into a variable rather than calling it repeatedly.
- Use it in audit triggers to group all changes made by one transaction.
- Combine it with
SESSION_CONTEXTorCONTEXT_INFOto add application-level context. - Remember that transaction IDs can be reused over time after the transaction completes; they are not a permanent unique key.
Troubleshooting
- Function not recognized. You are likely on SQL Server 2014 or earlier. Check
SELECT @@VERSION;and upgrade or use an alternative such assys.dm_tran_current_transaction. - Always returns NULL. Confirm you are inside an explicit transaction or that implicit transactions are enabled. Run
SELECT @@TRANCOUNT;to check. - Unexpected value. Another transaction may be active on the session due to a previous uncommitted statement. Roll back or commit and retry.
- Permission errors. The function itself needs no special permission, but the surrounding query may. Verify access to the target table or database.
- Different results in different databases. That is expected: transaction IDs are database-scoped. Run the query in the same database as your data.
Frequently Asked Questions
What does CURRENT_TRANSACTION_ID() return?
It returns the bigint transaction ID of the current transaction, or NULL if no transaction is active.
Is it available in SQL Server 2014?
No. It was introduced in SQL Server 2016. Use sys.dm_tran_current_transaction as a workaround on older versions.
Does it work in Azure SQL Database?
Yes, it is supported in Azure SQL Database and Azure SQL Managed Instance.
Can I use it in a DEFAULT constraint?
No, because it is nondeterministic. Populate the column with an INSERT or trigger instead.
Is the transaction ID unique across the server?
No, it is unique within a database. Do not rely on it as a server-wide identifier.
Why do I get the same ID for nested transactions?
SQL Server does not support true nested transactions; inner BEGIN TRANSACTION statements only increment @@TRANCOUNT, so the transaction ID stays the same.