Troubleshooting Guide: Error 11324 – @@TRANCOUNT Not Supported Inside ATOMIC Blocks
Why This Error Happens
This error occurs because natively compiled T-SQL modules (stored procedures, functions, or triggers) using ATOMIC blocks in SQL Server 2025 do not support legacy transaction-counting variables like @@TRANCOUNT. ATOMIC blocks manage their own internal transaction scope implicitly, so referencing @@TRANCOUNT, BEGIN TRANSACTION, COMMIT, or ROLLBACK explicitly inside them conflicts with the engine’s native compilation model. This typically surfaces when developers migrate interpreted T-SQL code into memory-optimized, natively compiled modules without refactoring transaction-handling logic.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Module
- Open SQL Server Management Studio (SSMS) and locate the stored procedure or function referenced in the error message.
- Run the following query to find all natively compiled modules in the current database:
SELECT name, object_id FROM sys.sql_modules WHERE uses_native_compilation = 1;
- Inspect the module definition using
sp_helptext 'YourProcedureName';to locate the@@TRANCOUNTreference.
Step 2: Remove Explicit Transaction Control from the ATOMIC Block
- Delete any references to
@@TRANCOUNT,BEGIN TRAN,COMMIT TRAN, orROLLBACK TRANfrom within theBEGIN ATOMIC ... ENDblock. - Confirm the
ATOMICblock options are correctly set, for example:
BEGIN ATOMIC WITH
(TRANSACTION ISOLATION LEVEL = SNAPSHOT,
LANGUAGE = N'us_english')
-- business logic here, no explicit transaction statements
END
- Allow the ATOMIC block to manage commit/rollback behavior automatically based on success or failure of the batch.
Step 3: Recompile and Redeploy the Module
- Drop and recreate the natively compiled procedure using
DROP PROCEDUREfollowed byCREATE PROCEDURE ... WITH NATIVE_COMPILATION. - Ensure the database compatibility level is set to 170 or higher:
ALTER DATABASE YourDatabase SET COMPATIBILITY_LEVEL = 170;
- Restart the SQL Server service via services.msc on Windows Server 2025 / Windows 11 if schema binding issues persist after redeployment.
Step 4: Validate Memory-Optimized Filegroup Configuration
- Verify the memory-optimized filegroup exists and is properly linked:
SELECT * FROM sys.filegroups WHERE type = 'FX';
- Confirm there are no orphaned checkpoint files causing compilation failures in
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\.
Alternative or Verification Steps
- Execute the corrected procedure manually (
EXEC YourProcedureName;) and confirm no Error 11324 is raised, then checksys.dm_exec_procedure_statsto confirm successful execution plans are cached. - If the module still fails, convert it to an interpreted T-SQL procedure (remove
NATIVE_COMPILATIONandATOMICclauses) as a temporary workaround while refactoring the transaction logic for native compatibility.