Error
Error Code:
1238
MariaDB Error 1238: Incorrect Variable Scope
Description
MariaDB Error 1238, 'Variable '%s' is a %s variable', indicates an attempt to modify or access a system variable using an incorrect scope keyword (e.g., attempting to set a GLOBAL variable using the SESSION keyword, or vice-versa). This error occurs when a variable is not configurable at the specified level.
Error Message
Variable '%s' is a %s variable
Known Causes
3 known causesSetting Global-Only Variable as Session
Occurs when a user tries to modify a system variable with the `SESSION` keyword, but the variable is only configurable at the `GLOBAL` level.
Setting Session-Only Variable as Global
This happens when a user attempts to change a system variable using the `GLOBAL` keyword, but the variable is exclusively a `SESSION` level setting.
Incorrect Scope for Dynamic Variables
Applying the wrong scope (`GLOBAL` or `SESSION`) to a variable that can be set at both levels, but the current context or variable type requires a specific scope, leading to a conflict.
Solutions
3 solutions available1. Identify and Correct Variable Scope in Stored Procedures/Functions medium
Locate the variable causing the scope issue and ensure it's declared and used within the correct block.
1
Examine the stored procedure or function code where the error is occurring. Look for variables declared using `DECLARE` or assigned values.
2
Identify the specific variable mentioned in the error message (e.g., '%s').
3
Determine if the variable is being accessed outside its declared scope. For example, a local variable declared within a `BEGIN...END` block cannot be accessed outside that block.
4
If the variable is intended to be global or accessible across different parts of the procedure, consider declaring it at the procedure level (outside any specific `BEGIN...END` blocks). If it's meant to be local, ensure it's only used within its intended scope.
DELIMITER //
CREATE PROCEDURE my_procedure(IN input_param INT)
BEGIN
DECLARE local_var INT;
SET local_var = input_param * 2;
-- Accessing local_var here is fine
SELECT local_var;
END //
DELIMITER ;
-- Attempting to access local_var outside the procedure will cause an error.
-- SELECT local_var; -- This would be incorrect.
5
Recompile or re-execute the stored procedure/function after making the necessary scope corrections.
2. Review User-Defined Variables and Session Scope easy
Check if you are attempting to use a user-defined variable outside the session where it was set.
1
User-defined variables in MariaDB are session-specific. This means a variable set in one client connection is not available in another.
2
If you are trying to access a variable that was set in a different session (e.g., in a script run by another user, or a different terminal), you will encounter this error. Ensure the variable is set within the current session before it's used.
SET @my_variable = 'some_value';
SELECT @my_variable;
3
If you need to pass values between sessions or applications, consider using temporary tables, application-level logic, or persistent storage mechanisms instead of session variables.
3. Ensure Correct Syntax for System Variables easy
Verify that system variables are referenced with the correct syntax and are not being modified incorrectly.
1
System variables in MariaDB are prefixed with `@@` (e.g., `@@session.sql_mode` or `@@global.max_connections`).
SELECT @@version;
2
If you are trying to set a system variable, ensure you are using the correct scope (`SESSION` or `GLOBAL`) and have the necessary privileges.
SET SESSION sql_mode = 'STRICT_TRANS_TABLES';
SET GLOBAL max_connections = 200;
3
The error 'Variable '%s' is a %s variable' can occur if you try to treat a system variable as a user-defined variable or vice-versa, or if you attempt to assign a value to a read-only system variable.
4
Consult the MariaDB documentation for the specific system variable you are trying to use to understand its scope, settability, and correct usage.