1952
MySQLERRORCommonStored RoutinesHIGH confidence
Variable already exists with this name
Production Risk
None — procedure/function creation is rejected.
What this means
A stored procedure or function declares a local variable with the same name as a parameter or an outer-scope variable, which is not allowed in MySQL's stored routine scope rules.
Why it happens
- 1DECLARE x INT inside a stored procedure that already has a parameter named x.
- 2Nested BEGIN...END blocks where the inner block re-declares a variable from the outer block.
How to reproduce
trigger — this will error
trigger — this will error
CREATE PROCEDURE p(x INT) BEGIN DECLARE x INT DEFAULT 0; END; -- shadow of parameter x
expected output
ERROR 1952 (42000): Variable 'x' already exists.
Fix
Rename the local variable to avoid shadowing
Rename the local variable to avoid shadowing
CREATE PROCEDURE p(x INT) BEGIN DECLARE local_x INT DEFAULT 0; END;
Why this works
Use distinct names for local variables and parameters within the same scope.
Sources
Official documentation ↗
MySQL 8.0 — 1952 ER_SP_BAD_VAR_SHADOW
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev