3286
MariaDBERRORNotableCTEsHIGH confidence

Recursive CTE must use UNION

Production Risk

Low — query is rejected; add the required UNION ALL.

How to reproduce
trigger — this will error
trigger — this will error
WITH RECURSIVE cte AS (SELECT 1 AS n SELECT n+1 FROM cte WHERE n < 10) SELECT * FROM cte;

expected output

ERROR 3286 (HY000): Recursive Common Table Expression 'cte' should contain a UNION.

Fix

Add UNION ALL

Add UNION ALL
WITH RECURSIVE cte AS (SELECT 1 AS n UNION ALL SELECT n+1 FROM cte WHERE n < 10) SELECT * FROM cte;

Why this works

UNION ALL separates the anchor member from the recursive member.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3286 ER_CTE_RECURSIVE_REQUIRES_UNION

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

← All MariaDB errors