3291
MariaDBERRORNotableCTEsHIGH confidence
Recursive CTE recursive member cannot use LIMIT
Production Risk
Low — query is rejected; move LIMIT to the outer SELECT.
How to reproduce
trigger — this will error
trigger — this will error
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n < 10 LIMIT 5) SELECT * FROM cte;
expected output
ERROR 3291 (HY000): LIMIT/OFFSET not allowed in recursive CTE query block.
Fix
Move LIMIT to outer query
Move LIMIT to outer query
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n < 10) SELECT * FROM cte LIMIT 5;
Why this works
LIMIT on the outer SELECT limits the rows returned without disrupting the recursion.
What not to do
✕
Sources
Official documentation ↗
MySQL 8.0 — 3291 ER_CTE_RECURSIVE_FORBIDS_LIMIT
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev