Common SQL Errors

MySQL Error 1765 ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER - Fix Guide

Galaxy Team
August 7, 2025

<p>MySQL raises error 1765 when a statement tries to modify a restricted system variable inside a stored function or trigger.</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 1765 ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER?

<p>MySQL Error 1765: ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER occurs when a statement inside a stored function or trigger tries to set a system variable that MySQL forbids in those contexts. Remove the SET statement or move it into permitted code outside the function or trigger to resolve the issue.</p>

Error Highlights

Typical Error Message

The system variable %s cannot be set in stored functions

Error Type

Execution Error

Language

MySQL

Symbol

ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER

Error Code

1765

SQL State

HY000

Explanation

Table of Contents

What is MySQL error 1765 ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER?

Error 1765 pops up when MySQL detects a SET statement targeting a system variable inside a stored function or trigger. The server blocks the action for safety and consistency reasons.

When does this error occur?

The error surfaces at creation time or runtime if the definition contains a prohibited SET statement, such as SET GLOBAL sql_mode = 'STRICT_ALL_TABLES'.

Why is it important to fix quickly?

Leaving the faulty code in place prevents the routine from compiling or running, halting application logic and breaking data-integrity automation.

What Causes This Error?

The root cause is using SET to change a system variable that MySQL marks as non-settable inside stored programs.

Attempts to change SQL modes, transaction isolation, or character sets within the routine trigger the error.

Code generators and migration scripts that wrap configuration changes in triggers frequently introduce the mistake.

How to Fix MySQL Error 1765

Identify the offending SET statement by reading the routine definition or running SHOW CREATE FUNCTION/ TRIGGER.

Move the variable change to client code, a stored procedure, or the session that invokes the routine.

If the variable must stay dynamic, pass the required value as a parameter instead of altering the global variable.

Common Scenarios and Solutions

Trying to enable strict SQL mode inside a trigger - remove the SET and enforce strict mode at the server or session level.

Changing autocommit in a function - wrap the data manipulation in a procedure where autocommit can be toggled safely.

Altering character_set_results in a trigger - move the change to the client connection string.

Best Practices to Avoid This Error

Keep stored functions and triggers pure: avoid session or global configuration changes inside them.

Perform environment-level settings in application code before calling database routines.

Use Galaxy's linting to flag disallowed SET statements during query authoring.

Related Errors and Solutions

Error 1385 (ER_VARIABLE_IS_READONLY) arises when attempting to modify a read-only variable anywhere, including procedures. The fix is identical: remove or relocate the SET statement.

Error 1229 (ER_VARIABLE_NOT_SETTABLE_IN_SF) predates 1765 and appears in older MySQL versions. Apply the same resolution steps.

Common Causes

Setting sql_mode inside a trigger

Developers often try to enforce strict or ANSI modes in the trigger code, triggering error 1765.

Changing transaction_isolation in a stored function

Isolation level changes are blocked in stored functions to protect transactional consistency.

Altering character set variables

SET NAMES or modifying character_set_results inside routines violates MySQL's restrictions.

Code generator defaults

Some ORMs generate SET statements automatically, which slip into routine bodies unnoticed.

Related Errors

ER_VARIABLE_IS_READONLY (Error 1385)

Raised when attempting to change a read-only system variable anywhere in SQL.

ER_VARIABLE_NOT_SETTABLE_IN_SF (Error 1229)

The predecessor of 1765, produced in MySQL 5.6 and earlier for the same issue.

ER_SPILLEGAL_SET (Error 1235)

Occurs when SET is used with an illegal parameter inside a stored program.

FAQs

Can I set any variable inside a stored function?

You can assign local variables, but MySQL blocks most system variables for safety.

Does the error apply to stored procedures?

Stored procedures have fewer restrictions, but some GLOBAL variables remain off-limits.

Which MySQL versions raise error 1765?

MySQL 5.7 and later map illegal SET operations in functions or triggers to error code 1765.

How does Galaxy help avoid this problem?

Galaxy's linter warns when you type SET GLOBAL or SET SESSION inside a routine, preventing the error before execution.

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