Error
Error Code:
1311
MariaDB Error 1311: Uninitialized Variable Reference
Description
MariaDB Error 1311 indicates that a variable is being accessed or used within a stored program (such as a procedure, function, or trigger) before it has been assigned an initial value. This typically leads to unpredictable behavior or incorrect results, as the variable's content is undefined.
Error Message
Referring to uninitialized variable %s
Known Causes
4 known causesVariable Not Initialized
A declared variable is used in an expression, condition, or assignment without first being given an explicit value.
Incomplete Code Paths
A variable might be initialized in some execution branches but not in others, leading to an uninitialized state for specific code paths.
Typographical Errors
A misspelling in a variable name can cause the system to refer to a different, undeclared, or uninitialized variable instead of the intended one.
Scope or Declaration Issues
Variables might be declared in a scope that is not accessible where they are being used, or they might be implicitly declared and uninitialized.
Solutions
4 solutions available1. Initialize Variables Explicitly easy
Ensure all variables used in queries are explicitly declared and assigned a value before use.
1
When using user-defined variables (prefixed with '@'), always assign them a value before referencing them in subsequent statements. This can be done using `SET` or within a `SELECT` statement.
SET @my_variable = 0;
SELECT @my_variable + some_column FROM your_table;
2
Alternatively, initialize within a `SELECT` statement. This is often useful for creating temporary variables for a single query.
SELECT @my_variable := 0, some_column FROM your_table WHERE @my_variable < some_other_column;
2. Review Stored Procedures and Functions medium
Identify and correct uninitialized local variables within stored routines.
1
Examine the code of your stored procedures and functions. Look for local variables (declared using `DECLARE`) that are used in calculations or comparisons without being assigned an initial value.
DELIMITER //
CREATE PROCEDURE my_procedure(IN input_param INT)
BEGIN
DECLARE my_local_var INT;
-- ERROR: my_local_var is used here before assignment
SET @result = my_local_var + input_param;
SELECT @result;
END //
DELIMITER ;
2
Add an explicit `SET` statement to initialize the variable at the beginning of its scope or before its first use.
DELIMITER //
CREATE PROCEDURE my_procedure(IN input_param INT)
BEGIN
DECLARE my_local_var INT;
SET my_local_var = 0; -- Initialize the variable
SET @result = my_local_var + input_param;
SELECT @result;
END //
DELIMITER ;
3. Check for Implicit Variable Usage in Triggers medium
Ensure variables within triggers are properly initialized.
1
Inspect the triggers associated with your tables. Similar to stored procedures, local variables within triggers must be declared and initialized before use.
DELIMITER //
CREATE TRIGGER my_trigger BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
DECLARE counter INT;
-- ERROR: counter is used here without initialization
IF counter > 10 THEN
-- some action
END IF;
END //
DELIMITER ;
2
Initialize the variable within the trigger's `BEGIN...END` block.
DELIMITER //
CREATE TRIGGER my_trigger BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
DECLARE counter INT;
SET counter = 0; -- Initialize the variable
IF counter > 10 THEN
-- some action
END IF;
END //
DELIMITER ;
4. Upgrade MariaDB Version advanced
Consider upgrading to a more recent MariaDB version to benefit from bug fixes.
1
While MariaDB aims for backward compatibility, specific versions might have introduced or fixed certain behaviors related to variable handling. Review the release notes for your current and target versions.
Optional: Consult the official MariaDB release notes for versions between your current and desired upgrade target.
2
Perform a standard MariaDB upgrade following the official documentation for your operating system. This typically involves backing up your data, stopping the MariaDB service, installing the new packages, and restarting the service.
Example for Debian/Ubuntu:
# Backup your data
mysqlbackup --defaults-file=/etc/mysql/my.cnf --backup-dir=/path/to/backup
# Stop MariaDB
sudo systemctl stop mariadb
# Install new packages (e.g., from a repository)
sudo apt update
sudo apt install mariadb-server mariadb-client
# Restart MariaDB
sudo systemctl start mariadb