// SQL Server error reference

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
Error message
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.

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 @@TRANCOUNT to 0.
  • A runtime error with XACT_ABORT ON, or a deadlock, rolled the transaction back before reaching COMMIT.
  • Unbalanced code: COMMIT in more places than BEGIN TRANSACTION, or a COMMIT without any BEGIN at all.

How to fix it

  1. Guard the commit and look for the earlier error.

    IF @@TRANCOUNT > 0 COMMIT TRANSACTION;
  2. Use TRY...CATCH so an error goes to the CATCH block instead of running into the COMMIT.

    BEGIN TRY
        BEGIN TRANSACTION;
        -- work
        COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
        THROW;
    END CATCH;
  3. 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

More transactions and locking errors

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.

// fewer errors, faster fixes

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.