<p>MySQL raises error 1765 when a statement tries to modify a restricted system variable inside a stored function or trigger.</p>
<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>
The system variable %s cannot be set in stored functions
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.
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'.
Leaving the faulty code in place prevents the routine from compiling or running, halting application logic and breaking data-integrity automation.
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.
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.
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.
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.
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.
Developers often try to enforce strict or ANSI modes in the trigger code, triggering error 1765.
Isolation level changes are blocked in stored functions to protect transactional consistency.
SET NAMES or modifying character_set_results inside routines violates MySQL's restrictions.
Some ORMs generate SET statements automatically, which slip into routine bodies unnoticed.
Raised when attempting to change a read-only system variable anywhere in SQL.
The predecessor of 1765, produced in MySQL 5.6 and earlier for the same issue.
Occurs when SET is used with an illegal parameter inside a stored program.
You can assign local variables, but MySQL blocks most system variables for safety.
Stored procedures have fewer restrictions, but some GLOBAL variables remain off-limits.
MySQL 5.7 and later map illegal SET operations in functions or triggers to error code 1765.
Galaxy's linter warns when you type SET GLOBAL or SET SESSION inside a routine, preventing the error before execution.