1472
MySQLERRORNotableStored RoutinesHIGH confidence

Recursive stored procedure exceeded max_sp_recursion_depth

Production Risk

Medium — the stored procedure call will fail; review recursion logic.

What this means

ER_SP_RECURSION_LIMIT (1472, SQLSTATE HY000) is raised when a recursive stored procedure call exceeds the limit set by max_sp_recursion_depth.

Why it happens
  1. 1Recursive stored procedure called more times than max_sp_recursion_depth allows
  2. 2max_sp_recursion_depth set to 0 (default — recursion disabled)
  3. 3Infinite or runaway recursion in the stored procedure
How to reproduce
trigger — this will error
trigger — this will error
SET max_sp_recursion_depth = 5;
CALL recursive_proc(10);  -- Exceeds depth of 5

expected output

ERROR 1472 (HY000): Recursive limit 5 (as set by the max_sp_recursion_depth variable) was exceeded for routine 'recursive_proc'

Fix

Increase max_sp_recursion_depth or rewrite iteratively

Increase max_sp_recursion_depth or rewrite iteratively
-- Allow more recursion:
SET max_sp_recursion_depth = 50;

-- Or rewrite using a loop (preferred):
CREATE PROCEDURE iterative_proc(IN depth INT)
BEGIN
  DECLARE i INT DEFAULT 0;
  WHILE i < depth DO
    -- logic here
    SET i = i + 1;
  END WHILE;
END;

Why this works

max_sp_recursion_depth controls the maximum recursive depth; iterative approaches avoid this limitation entirely.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 1472 ER_SP_RECURSION_LIMIT

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All MySQL errors