Error
Error Code: 1327

MariaDB Error 1327: Undeclared Variable in Stored Routine

📦 MariaDB
📋

Description

Error 1327 indicates that a variable was referenced within a stored program (e.g., stored procedure, function, trigger) without being explicitly declared using the `DECLARE` statement. This prevents the database from allocating memory or understanding the data type for the variable, leading to a compilation or runtime failure of the routine.
💬

Error Message

Undeclared variable: %s
🔍

Known Causes

3 known causes
⚠️
Missing Variable Declaration
A variable was used in a stored procedure, function, or trigger without a preceding `DECLARE` statement to define its name and data type.
⚠️
Typographical Error in Variable Name
A variable was declared, but later referenced with a slight spelling mistake, making it appear as a new, undeclared variable to the parser.
⚠️
Variable Scope Mismatch
A variable was declared within a specific `BEGIN...END` block, but then referenced outside its defined scope, where it is no longer recognized.
🛠️

Solutions

3 solutions available

1. Declare the Variable Before Use easy

Explicitly declare any variable before it's assigned a value or used within a stored routine.

1
Review your stored routine (stored procedure or function) code. Identify the variable mentioned in the error message (represented by `%s`).
2
Add a `DECLARE` statement for the variable at the beginning of the routine, before its first use. Specify the data type for the variable.
DECLARE variable_name data_type;
-- Example: DECLARE my_count INT;
-- Example: DECLARE status_message VARCHAR(255);
3
Recompile or re-execute the stored routine. Ensure the `CREATE PROCEDURE` or `CREATE FUNCTION` statement is executed correctly.
DELIMITER //
CREATE PROCEDURE my_procedure() 
BEGIN
    DECLARE my_variable INT;
    SET my_variable = 10;
    SELECT my_variable;
END //
DELIMITER ;

CALL my_procedure();

2. Correct Typo in Variable Name easy

Verify that the variable name used in the routine exactly matches its declaration, checking for typos.

1
Locate the specific variable name that caused the 'Undeclared variable: %s' error. This is the `%s` part of the error message.
2
Examine the `DECLARE` statement for that variable within the stored routine. Compare the spelling and case sensitivity (though MariaDB variable names are generally case-insensitive, it's good practice to be consistent).
3
If a typo is found, correct it in either the `DECLARE` statement or where the variable is being used. It's generally best to correct the usage to match the declaration.
-- Incorrect usage:
-- SET my_varible = 5; -- Typo in 'my_varible'

-- Corrected usage:
SET my_variable = 5; -- Matches DECLARE my_variable INT;
4
Recreate or modify the stored routine with the corrected variable name.
DELIMITER //
CREATE PROCEDURE correct_typo_proc()
BEGIN
    DECLARE correct_name VARCHAR(50);
    SET correct_name = 'Hello';
    SELECT correct_name;
END //
DELIMITER ;

CALL correct_typo_proc();

3. Ensure Variable is Declared Within the Correct Scope medium

Verify that the variable is declared within the same block of code (e.g., a `BEGIN...END` block) where it's being used, or in an outer scope.

1
Understand the scope of variables in MariaDB stored routines. Variables declared within a `BEGIN...END` block are local to that block. Variables declared at the routine level are accessible throughout the routine.
2
If the error occurs within a nested `BEGIN...END` block, ensure the variable is declared either at the beginning of that inner block or in an outer block that encompasses it.
-- Incorrect: Variable declared in outer scope, used in inner scope without redeclaration.
DELIMITER //
CREATE PROCEDURE scope_error_proc()
BEGIN
    DECLARE outer_var INT;
    SET outer_var = 1;
    IF outer_var > 0 THEN
        BEGIN
            -- Undeclared variable: outer_var (if not declared in inner scope)
            SET outer_var = outer_var + 1;
            SELECT outer_var;
        END;
    END IF;
END //
DELIMITER ;

-- Corrected: Declare in outer scope or redeclare in inner scope.
DELIMITER //
CREATE PROCEDURE scope_fix_proc()
BEGIN
    DECLARE outer_var INT;
    SET outer_var = 1;
    IF outer_var > 0 THEN
        BEGIN
            -- Option 1: Use the outer_var directly if accessible
            SET outer_var = outer_var + 1;
            SELECT outer_var;
        END;
    END IF;
END //
DELIMITER ;

CALL scope_fix_proc();
3
If you intend to use a variable declared in an outer scope within an inner scope, ensure it's accessible. MariaDB allows this by default if the inner block doesn't redeclare a variable with the same name.
4
Recreate the stored routine after adjusting declarations to ensure proper scoping.
🔗

Related Errors

5 related errors