recursion limit SQL Origin: Data OSI layer 7 · Application Last updated 12 Oct 2026

Msg 530: The statement terminated. The maximum recursion 100 has been exhausted before statement completion. / MySQL 3636 Recursive query aborted after 1001 iterations

A recursive CTE exceeded the database’s recursion limit — often an infinite loop in hierarchical data.

Exact message you may see The maximum recursion 100 has been exhaustedRecursive query aborted after
Seen on: SQL

Meaning

Cycles in parent/child data (a node that is its own ancestor) or missing termination conditions make recursion endless.

Common causes

  • Cycle in hierarchical data
  • Missing stop condition in the recursive part
  • Legitimately deep hierarchies over the default limit

⚡ Quick fix

  1. Detect cycles (track visited ids/path)
  2. Add a depth limit column
  3. Raise the limit (OPTION (MAXRECURSION n), cte_max_recursion_depth) only for legitimate depth

Detailed fix by platform

SQL

  1. WITH RECURSIVE t AS (SELECT id, parent_id, 1 depth FROM nodes WHERE id = 1 UNION ALL SELECT n.id, n.parent_id, t.depth + 1 FROM nodes n JOIN t ON n.parent_id = t.id WHERE t.depth < 50) SELECT * FROM t;

How to diagnose

  1. Data — Any cycles?
  2. Depth — Expected maximum?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

1136 — MySQL ERROR 1136 (21S01): Column count doesn't match value count at row 1 (SQL)1136aggregate in WHERE — ERROR: aggregate functions are not allowed in WHERE (use HAVING) / MySQL 1111 Invalid use of group function (SQL)aggregate in WHEREGROUP BY — Column must appear in the GROUP BY clause / isn't in GROUP BY (MySQL 1055 only_full_group_by) (SQL)GROUP BYMaximum function nesting — Fatal error: Maximum function nesting level of '256' reached, aborting! (PHP)Maximum function nest…RangeError — RangeError: Maximum call stack size exceeded (JavaScript)RangeErrorRecursionError — RecursionError: maximum recursion depth exceeded (Python)RecursionErrorStack overflow — .NET: Stack overflow. / Process is terminated due to StackOverflowException (.NET)Stack overflowStackOverflowError — java.lang.StackOverflowError (Java)StackOverflowErrorrecursion…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.