Common SQL Errors

MySQL Error 1311 ER_SP_UNINIT_VAR: Referring to Uninitialized Variable – Fix and Prevent

Galaxy Team
August 6, 2025

Error 1311 occurs when a MySQL stored program references a local, INOUT, or OUT variable before it has 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 means a stored procedure, trigger, or function is using a variable that has never been set. Initialize the variable with DECLARE ... DEFAULT, SET, or SELECT ... INTO before the first read 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 raises Error 1311 when a stored routine, trigger, or event references a variable that has not yet been initialized.

The server prevents reading an undefined value to avoid unpredictable results.

The error appears immediately after the first read of the uninitialized variable, halting the statement and rolling back the current block if necessary.

When does this error occur?

The error surfaces in MySQL 5.7, 8.0, and later whenever a local variable, INOUT parameter, or OUT parameter is read before a value is assigned with SET, SELECT ...

INTO, or a DEFAULT clause.

It frequently happens inside IF conditions, cursor loops, and expressions that attempt to concatenate or compare an uninitialized variable.

Why is it important to fix?

Leaving variables uninitialized risks null propagation, wrong calculations, and inconsistent business logic. Fixing the issue ensures deterministic results and cleaner code.

.

Common Causes

Related Errors

FAQs

Does Error 1311 always stop the procedure?

Yes. MySQL aborts the current statement and propagates the error up the call stack, rolling back only the statement, not the entire transaction unless autocommit is off.

Can I disable this check?

No. MySQL does not provide a session variable to ignore uninitialized variables. Proper initialization is required.

Does NULL count as initialized?

Yes. If you explicitly SET a variable to NULL, it is considered initialized. The error only appears when you read a variable that has never been assigned any value.

How can Galaxy help?

Galaxy’s linting engine detects uninitialized local variables in stored program scripts and highlights them in the editor before you execute the code.

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