MySQL raises ER_SP_DUP_VAR when a stored routine defines two parameters or variables with the same name.
MySQL Error 1331: ER_SP_DUP_VAR appears when a stored procedure or function contains duplicate variable or parameter names. Rename or remove the second declaration, then re-create the routine to resolve the issue.
Duplicate variable: %s
MySQL throws Error 1331 (SQLSTATE 42000) with the message “Duplicate variable: %s” when it detects two routine parameters, local variables, or condition/handler labels that share the same identifier.
The error halts CREATE PROCEDURE, CREATE FUNCTION, or ALTER ROUTINE statements because MySQL cannot differentiate between identically named symbols inside a single scope.
The error arises during routine parsing, before execution. It is common while adding new parameters, copying code, or refactoring logic where a variable is accidentally redeclared.
Developers often hit it after pasting sample code into Galaxy or any SQL editor without auditing existing DECLARE lines and IN/OUT parameter lists.
Leaving duplicate names unresolved blocks routine creation, breaks deployments, and can trigger CI/CD pipeline failures. Quick remediation ensures database migrations run cleanly and keeps application releases on schedule.
Duplicate names in the parameter list such as IN user_id INT, IN user_id INT immediately trigger the error.
Declaring a local variable that matches a parameter, for example DECLARE user_id INT; inside the routine body, also raises ER_SP_DUP_VAR.
Copy-pasted HANDLER or CURSOR names that collide with existing variables fall under the same rule.
Identify every duplicate symbol in the routine definition. Use SHOW CREATE PROCEDURE or your editor’s syntax highlighting to scan the parameter list and DECLARE section.
Rename or drop the redundant declaration, then recreate or alter the routine. Ensure each name is unique within the routine scope.
Same parameter twice – Remove one occurrence or rename it.
Parameter vs. local variable – Prefix local variables (e.g., v_user_id) to distinguish them from parameters.
Label collision – Change LOOP or HANDLER labels that match variable names.
Adopt consistent naming conventions: prefix parameters with p_ and variables with v_.
Leverage Galaxy’s linter, which flags duplicate declarations in real time, preventing commits with ER_SP_DUP_VAR.
Review routine diffs in code review to catch accidental copy-paste duplicates.
Error 1304 ER_SP_DUP_COND – Duplicate condition handler. Ensure unique condition names.
Error 1303 ER_SP_LABEL_REDEFINE – Duplicate labels. Rename conflicting LOOP/REPEAT labels.
Error 1330 ER_SP_DUP_PARAM – Duplicate parameters in routine definition. Remove the extra parameter.
Listing the same parameter twice in the IN/OUT/INOUT clause instantly triggers ER_SP_DUP_VAR.
Declaring a variable with the same name as an existing parameter causes a conflict during parsing.
Importing code samples without renaming variables often introduces duplicates.
DECLARE CONTINUE HANDLER or OPEN cursor names matching variables also raise the error.
Raised when the parameter list itself contains duplicates. Resolve by ensuring each parameter name is unique.
Occurs on duplicate condition handler names. Rename conflicting handlers.
Triggered when a LOOP or block label is declared twice within the same scope.
No. MySQL enforces unique identifiers in stored routines for reliability. The only fix is to rename or remove duplicates.
Identifier comparison follows the lower_case_table_names setting. On most systems, MySQL treats routine identifiers as case-insensitive, so MyVar and myvar clash.
Yes. ALTER PROCEDURE re-parses the full definition, so duplicates introduced during alteration still trigger Error 1331.
Galaxy’s SQL editor highlights duplicate declarations instantly and blocks saving routines that would fail with ER_SP_DUP_VAR, preventing deploy-time surprises.