Common SQL Errors

MySQL Error 1766: ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION - How to Fix and Prevent

Galaxy Team
August 7, 2025

<p>MySQL error 1766 occurs when you try to change a protected system variable while a transaction is still open.</p>

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error 1766 (ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION)?

<p>MySQL Error 1766: ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION means the server blocks SET statements on certain system variables during an active transaction. Commit or roll back the transaction, then issue the SET, or move the change to session start to resolve the problem.</p>

Error Highlights

Typical Error Message

The system variable %s cannot be set when there is an

Error Type

Transaction Error

Language

MySQL

Symbol

ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION

Error Code

1766

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 1766 (ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION)?

MySQL raises error 1766 when a client issues SET GLOBAL or SET SESSION against a system variable that the engine forbids modifying while a transaction is open. The message returned is: The system variable %s cannot be set when there is an ongoing transaction.

The restriction exists to protect transactional consistency. Changing critical parameters such as autocommit, foreign_key_checks, or sql_mode mid-transaction could yield unpredictable results, so the server rejects the request.

What Causes This Error?

The error fires whenever a protected variable is targeted by a SET command after START TRANSACTION or when autocommit is OFF. It also appears if your client library implicitly begins a transaction, for example by selecting from InnoDB with autocommit disabled.

Variables most often involved include autocommit, tx_isolation, transaction_read_only, and other engine-level flags that impact transaction semantics.

How to Fix MySQL Error 1766

First, end the current transaction with COMMIT or ROLLBACK, then run the SET statement. Alternatively, move the SET command to the very start of the session, before any data-modifying statement, or apply the change globally outside peak hours.

If the variable must change automatically, wrap your logic in stored procedures that explicitly commit before issuing SET commands, ensuring no active transaction exists.

Common Scenarios and Solutions

Application starts a session, disables autocommit, runs multiple inserts, then attempts SET autocommit=1 mid-batch. Solution: commit first, or reorder the SET to the beginning.

DBA script loops through databases, sets sql_mode, and forgets an earlier START TRANSACTION. Add an explicit COMMIT before the loop or remove unnecessary START TRANSACTION.

Best Practices to Avoid This Error

Always commit or roll back promptly. Avoid changing transaction-sensitive variables mid-session. Place all session-level SET statements immediately after connection creation. Monitor long-running transactions with performance_schema tables and kill idle ones.

Related Errors and Solutions

MySQL error 1178 (SQLSTATE HY000) disallows changing foreign_key_checks within a transaction. Error 1568 blocks ALTER TABLE on temporary tables in transactions. Solutions are similar: end the transaction first.

Common Causes

Changing autocommit mid-transaction

SET autocommit=1 or SET autocommit=0 after START TRANSACTION triggers the error.

Modifying isolation level inside a transaction

SET SESSION transaction_isolation='READ COMMITTED' while a transaction is active is blocked.

Disabling foreign key checks during bulk load

SET foreign_key_checks=0 fails if the session already opened a transaction.

Implicit transactions via connectors

Some drivers start transactions automatically when autocommit is disabled, causing unexpected 1766 errors.

Related Errors

Error 1178: foreign_key_checks disallowed within transaction

Occurs when toggling foreign_key_checks during an open transaction. Solution is identical: commit first.

Error 1568: Altering temporary table with transaction

Blocking ALTER TABLE actions on temp tables while a transaction is active.

Error 1205: Lock wait timeout exceeded

Long-running uncommitted transactions may also lead to lock timeouts; committing sooner avoids both issues.

FAQs

Can I safely ignore error 1766?

No. Ignoring it leaves the variable unchanged, which might break application logic. Always correct your transaction flow.

Which MySQL versions return error 1766?

MySQL 5.7, 8.0, and MariaDB 10.2+ enforce this rule.

Does SET GLOBAL cause the same error?

Yes, if the session that issues SET GLOBAL has an active transaction.

How does Galaxy help avoid 1766?

Galaxy highlights open transactions and offers AI-generated fixes, reminding you to commit before issuing SET statements.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo