3287
MariaDBERRORNotableCTEsHIGH confidence
Recursive CTE must have non-recursive anchor first
Production Risk
Low — query is rejected; reorder UNION members.
How to reproduce
trigger — this will error
trigger — this will error
WITH RECURSIVE cte AS (SELECT n+1 FROM cte WHERE n < 10 UNION ALL SELECT 1 AS n) SELECT * FROM cte;
expected output
ERROR 3287 (HY000): Recursive CTE 'cte' must have non-recursive first member before recursive one.
Fix
Anchor first
Anchor first
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n < 10) SELECT * FROM cte;
Why this works
The non-recursive anchor SELECT must be the first member of the UNION ALL.
What not to do
✕
Sources
Official documentation ↗
MySQL 8.0 — 3287 ER_CTE_RECURSIVE_REQUIRES_NONRECURSIVE_FIRST
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev