Common SQL Errors

MySQL Error 1311: ER_SP_UNINIT_VAR - How to Fix and Prevent

Galaxy Team
August 6, 2025

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.

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 1311 (ER_SP_UNINIT_VAR)?

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.

Error Highlights

Typical Error Message

Referring to uninitialized variable %s

Error Type

Runtime Error

Language

MySQL

Symbol

ER_SP_UNINIT_VAR

Error Code

1311

SQL State

1000

Explanation

Table of Contents

What is MySQL error 1311 (ER_SP_UNINIT_VAR)?

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.

When does the error occur?

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.

Why is it important to fix?

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.

Common Causes

Omitted initialization

A new DECLARE var INT; line is introduced without a subsequent SET var = 0; before first use.

Conditional branches

One control-flow path initializes the variable, but another path reaches a reference first, triggering the error.

SELECT ... INTO mismatch

A SELECT ... INTO statement fails to return a row, leaving the target variable unassigned.

Loop counters

FOR or WHILE loops referencing a counter that was declared but never seeded.

Related Errors

MySQL Error 1321: ER_SP_WRONG_NAME

Raised when a stored routine name is invalid. Fix by using valid identifiers.

MySQL Error 1327: ER_SP_FIELD_TYPE_MISMATCH

Occurs when SELECT ... INTO target types do not match source columns.

MySQL Error 1338: ER_SP_BADSELECT

Thrown when a SELECT inside a routine returns multiple rows into a single variable.

FAQs

Does MySQL auto-initialize local variables to NULL?

No. MySQL requires an explicit DEFAULT clause or SET statement. Reading an unassigned variable triggers ER_SP_UNINIT_VAR.

Can I disable the error with sql_mode?

No. ER_SP_UNINIT_VAR is hard-coded. The only remedy is to initialize variables.

Which MySQL versions raise this error?

All supported versions from 5.0 onward enforce variable initialization inside stored routines.

How does Galaxy help?

Galaxy’s editor warns about uninitialized variables in real time and offers AI-generated fixes, reducing runtime failures.

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