MySQL raises ER_SP_UNINIT_VAR when a stored procedure, function, or trigger refers to a local variable that has not been assigned a value.
MySQL Error 1311: ER_SP_UNINIT_VAR appears when a stored routine reads a variable before assigning a value. Initialize the variable with SET or SELECT ... INTO before first use to resolve the error.
Referring to uninitialized variable %s
MySQL throws error 1311 with the message “Referring to uninitialized variable ” when a stored procedure, function, or trigger attempts to read a local variable that has never been assigned. The server halts the routine at the offending line.
This runtime error surfaces only inside stored routines because MySQL expects developers to explicitly assign every local variable before usage. Uninitialized reads can lead to nondeterministic results, so the server treats them as fatal.
The error fires during execution, not compilation. A CREATE PROCEDURE statement may succeed, but the first call will fail if any branch can reach a SET, SELECT, or IF expression that references an uninitialized variable.
The issue is common after adding new local variables, refactoring code paths, or porting logic from databases that auto-initialize variables to NULL.
Ignoring ER_SP_UNINIT_VAR stops business logic, breaks nightly jobs, and leads to partial data writes. Fixing the error restores routine reliability and prevents silent data corruption.
A new DECLARE var INT; line is introduced without a subsequent SET var = 0; before first use.
One control-flow path initializes the variable, but another path reaches a reference first, triggering the error.
A SELECT ... INTO statement fails to return a row, leaving the target variable unassigned.
FOR or WHILE loops referencing a counter that was declared but never seeded.
Raised when a stored routine name is invalid. Fix by using valid identifiers.
Occurs when SELECT ... INTO target types do not match source columns.
Thrown when a SELECT inside a routine returns multiple rows into a single variable.
No. MySQL requires an explicit DEFAULT clause or SET statement. Reading an unassigned variable triggers ER_SP_UNINIT_VAR.
No. ER_SP_UNINIT_VAR is hard-coded. The only remedy is to initialize variables.
All supported versions from 5.0 onward enforce variable initialization inside stored routines.
Galaxy’s editor warns about uninitialized variables in real time and offers AI-generated fixes, reducing runtime failures.