SQL Server error 3902: COMMIT has no corresponding BEGIN
COMMIT TRANSACTION ran when no transaction was open, usually because an earlier error, trigger or ROLLBACK already ended it.
- Error number
3902- Severity
- 16
- Category
- Transactions and locking
What it means
COMMIT needs an open transaction (@@TRANCOUNT > 0). If the transaction was already rolled back, by an error with XACT_ABORT on, a ROLLBACK in a nested procedure or trigger, or a deadlock, the COMMIT that follows has nothing to commit and raises 3902. The real problem is whatever ended the transaction early.
Common causes
- A nested procedure or trigger ran
ROLLBACK, which rolls back to the outermost transaction and sets@@TRANCOUNTto 0. - A runtime error with
XACT_ABORT ON, or a deadlock, rolled the transaction back before reachingCOMMIT. - Unbalanced code:
COMMITin more places thanBEGIN TRANSACTION, or aCOMMITwithout anyBEGINat all.
How to fix it
-
Guard the commit and look for the earlier error.
IF @@TRANCOUNT > 0 COMMIT TRANSACTION;
-
Use
TRY...CATCHso an error goes to theCATCHblock instead of running into theCOMMIT.BEGIN TRY BEGIN TRANSACTION; -- work COMMIT TRANSACTION; END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION; THROW; END CATCH; -
In nested procedures, roll back to a savepoint instead of the whole transaction (see error 266).
Reproduce it safely
COMMIT TRANSACTION; -- Msg 3902
Related errors
3903ROLLBACK has no corresponding BEGIN266Transaction count mismatch after EXECUTE3930Transaction cannot be committed3609Transaction ended in the trigger
More transactions and locking errors
1205Deadlocked and chosen as the victim1222Lock request time out period exceeded3960Snapshot isolation update conflict3616Error raised during trigger execution6401No transaction or savepoint found3998Uncommittable transaction rolled back3621The statement has been terminated
The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.
Work on SQL Server in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.