How to fix ERROR 1204: Only constant expressions with SET in MySQL

MySQLINTERMEDIATEMEDIUMUpdated Dec 23, 2025· 126 views

MySQL Error 1204 occurs when you try to assign a non-constant expression (function, subquery, or variable) to a SET statement that requires a literal value. Use constant values directly or SELECT...INTO syntax instead.

Quick fix

Fixes 1204

SET @myvar = NOW();           -- ERROR: Function not allowed
SET @var1 = @var2;             -- ERROR: Variable assignment
SET @result = (SELECT ...);    -- ERROR: Subquery not allowed
  1. Identify the problematic SET statement
  2. Replace SET with SELECT...INTO for query results
  3. Use SELECT instead of SET for functions
  4. Avoid assigning variables to other variables with SET
mysql — error
SET @myvar = NOW(); -- ERROR: Function not allowed
SET @var1 = @var2; -- ERROR: Variable assignment
SET @result = (SELECT ...); -- ERROR: Subquery not allowed
ERROR 1204: Only constant expressions with SET

What this error means

MySQL Error 1204 (ER_SET_CONSTANTS_ONLY) enforces a strict requirement that certain SET statements can only use constant expressions. This error is raised when you attempt to assign a non-constant expression—such as a function call, subquery result, or another variable—where MySQL expects a literal constant value. The error typically occurs with system variables that have restrictions on what types of expressions can be assigned to them, or when using user-defined variables in contexts that require compile-time constants.

Typical symptoms

  • MySQL query fails with "ERROR 1204: Only constant expressions with SET"
  • SET statement using a function like NOW(), UUID(), or CONCAT() triggers the error
  • Assigning one variable to another with SET fails
  • Subqueries in SET statements cause the error
  • Applications crash or queries are rejected during execution

Common causes

  1. Using a function (NOW(), UUID(), CONCAT(), etc.) in a SET statement that requires constants
  2. Attempting to assign one user-defined variable to another with SET @var1 = @var2
  3. Using a subquery result in a SET assignment
  4. Assigning column values or expressions to variables in SET
  5. Using SET with system variables that do not accept dynamic expressions
  6. Storing query results in a variable using SET instead of SELECT...INTO

How to fix "ERROR 1204: Only constant expressions with SET"

1Identify the problematic SET statement

Locate the exact line causing Error 1204 in your MySQL logs or application output. Look for patterns like:

sql
SET @myvar = NOW();           -- ERROR: Function not allowed
SET @var1 = @var2;             -- ERROR: Variable assignment
SET @result = (SELECT ...);    -- ERROR: Subquery not allowed

Note which expression (function, subquery, or variable) is being used.

2Replace SET with SELECT...INTO for query results

When you need to store a query result in a variable, use SELECT...INTO instead of SET:

Before (causes Error 1204):

sql
SET @maxId = (SELECT MAX(id) FROM users);

After (correct syntax):

sql
SELECT MAX(id) INTO @maxId FROM users;

This syntax is specifically designed for storing single values from queries into variables.

3Use SELECT instead of SET for functions

For functions like NOW(), UUID(), or CONCAT(), use SELECT assignment or use the value directly in your query:

Option 1: SELECT assignment (if you need to reuse the value):

sql
SELECT NOW() INTO @currentTime;
SELECT UUID() INTO @newId;

Option 2: Use the function directly in the query (preferred):

sql
INSERT INTO events (id, timestamp) VALUES (UUID(), NOW());
UPDATE users SET last_login = NOW() WHERE id = 1;

Using functions directly avoids variable assignment overhead and is more efficient.

4Avoid assigning variables to other variables with SET

User-defined variables cannot be assigned to other variables using SET. If you need to copy a variable:

Before (causes Error 1204):

sql
SET @sourceVar = 'some value';
SET @destVar = @sourceVar;  -- ERROR!

After (correct):

sql
SET @sourceVar = 'some value';
SET @destVar = 'some value';  -- Use constant

Or use SELECT to reference the value:

sql
SELECT @sourceVar INTO @destVar;
5Use prepared statements for dynamic system variable assignments

If you need to set a system variable with a dynamic value, use prepared statements:

Before (causes Error 1204):

sql
SET @value = 1000;
SET GLOBAL max_connections = @value;  -- ERROR!

After (using prepared statements):

sql
SET @sql = CONCAT('SET GLOBAL max_connections = ', 1000);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Prepared statements allow you to dynamically construct and execute SET statements that would otherwise require constants.

Advanced notes

MySQL Version Differences: Restrictions on constant expressions vary slightly across MySQL versions. Newer versions (8.0+) may have relaxed some constraints compared to earlier versions, so testing your code against your specific MySQL version is important.

User-Defined Variables vs System Variables: User-defined variables (starting with @) have different rules than system variables (like max_connections, global_max, etc.). System variables often have stricter requirements that only allow constant expressions. Always check MySQL documentation for the specific variable you're working with.

Performance Consideration: Using SELECT...INTO instead of SET with functions is generally more efficient because MySQL can optimize it better. When possible, use functions directly in INSERT/UPDATE/SELECT statements rather than storing them in variables first.

Transactions and Variable Scope: Variables set within a transaction remain in scope after COMMIT, but using expressions within a transaction that require constants follows the same rules. If you're working with multiple statements, consider the order and ensure each statement follows the constant expression rules.

Frequently asked questions

What does "1204" mean?
MySQL Error 1204 (ER_SET_CONSTANTS_ONLY) enforces a strict requirement that certain SET statements can only use constant expressions. This error is raised when you attempt to assign a non-constant expression—such as a function call, subquery result, or another variable—where MySQL expects a literal constant value. The error typically occurs with system variables that have restrictions on what types of expressions can be assigned to them, or when using user-defined variables in contexts that require compile-time constants.
How do I fix "1204"?
MySQL Error 1204 occurs when you try to assign a non-constant expression (function, subquery, or variable) to a SET statement that requires a literal value. Use constant values directly or SELECT...INTO syntax instead.
What causes "1204"?
Using a function (NOW(), UUID(), CONCAT(), etc.) in a SET statement that requires constants Attempting to assign one user-defined variable to another with SET @var1 = @var2 Using a subquery result in a SET assignment
Is the "1204" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading