Skip to main content
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
smpa01
Super User
Super User

Recursion Strategy

How can I perform recursive queries currently?

I am getting Recursive CTEs are unsupported in this version of Synapse SQL.

 

Is Recursion on cards?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
2 ACCEPTED SOLUTIONS
AndyDDC
Super User
Super User

Hi   at the moment there is no support for recursion in CTEs in the roadmap.  This was something that was requested in Synapse Serverless (which the lakehouse sql endpoint and warehouse is based on) but never happened.  What's new and planned for Synapse Data Warehouse in Microsoft Fabric - Microsoft Fabric | Microsoft...

 

In terms of what you can do for recursion, I write a query in which I join the same table back on itself.  E.G here's an example of basic recursion in an employees table.  This example can be used in the Warehouse, but can also be used in the lakehouse sql endpoint if the tables existed (eg created by spark).

 

create table employees
(
    empid int,
    empname varchar(50),
    managerid int
);

insert into employees
values (1,'andy',null),(2,'dave',1),(3,'alice',1),(4,'glenda',3)

select
    a.empname as EmployeeName,
    b.empname as ManagerName
from employees a
left join employees b on b.empid = a.managerid 

---------------------------------------------------------------

If my reply has been useful please consider providing kudos

and marking as the solution to help others users

---------------------------------------------------------------

View solution in original post

The reason recursion is supported in Datamart is that its backended by Azure SQL Database while the Lakehouse SQL Endpoint and Warehouse are a new MPP engine (started with Synapse Serverless).  With Fabric, MS have built a SQL engine from scratch with fundamentally different architecture to SQL Server/Azure SQL Database so not everything we expect will be there right now, if at all.

View solution in original post

5 REPLIES 5
AndyDDC
Super User
Super User

Hi   at the moment there is no support for recursion in CTEs in the roadmap.  This was something that was requested in Synapse Serverless (which the lakehouse sql endpoint and warehouse is based on) but never happened.  What's new and planned for Synapse Data Warehouse in Microsoft Fabric - Microsoft Fabric | Microsoft...

 

In terms of what you can do for recursion, I write a query in which I join the same table back on itself.  E.G here's an example of basic recursion in an employees table.  This example can be used in the Warehouse, but can also be used in the lakehouse sql endpoint if the tables existed (eg created by spark).

 

create table employees
(
    empid int,
    empname varchar(50),
    managerid int
);

insert into employees
values (1,'andy',null),(2,'dave',1),(3,'alice',1),(4,'glenda',3)

select
    a.empname as EmployeeName,
    b.empname as ManagerName
from employees a
left join employees b on b.empid = a.managerid 

---------------------------------------------------------------

If my reply has been useful please consider providing kudos

and marking as the solution to help others users

---------------------------------------------------------------

I don't think it is easy to come up with an alternative of conventional cte recursion for all scenarios. There are situations which needs both recursive ctes and cursors. The funny part is cte recursion is supported in datamart but not in Fabric artifacts.

 

I am yet to test if cursor is supported.

 

TBH they are integral part of data engineering and MS must consider adding support for them in the Fabric elemnets that have sql endpoints.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

The reason recursion is supported in Datamart is that its backended by Azure SQL Database while the Lakehouse SQL Endpoint and Warehouse are a new MPP engine (started with Synapse Serverless).  With Fabric, MS have built a SQL engine from scratch with fundamentally different architecture to SQL Server/Azure SQL Database so not everything we expect will be there right now, if at all.

vicenteajm21212
Frequent Visitor

Hello @smpa01 and @AndyDDC  muy buenos dias , puedes utilizar , esta opcion:

DECLARE @FechaInicio DATE = 'fechainicio';
DECLARE @FechaFin DATE = 'fechafin';
-- 3Insertar datos en la tabla de calendario usando una tabla de nรบmeros eficiente
WITH Numeros AS (
    SELECT TOP (DATEDIFF(DAY, @FechaInicio, @FechaFin) + 1)
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1 AS Num
    FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS X(n)       -- 10 filas
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS Y(n) -- 10*10 = 100 filas
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS Z(n) -- 100*10 = 1.000 filas
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS W(n) -- 1.000*10 = 10.000 filas
    CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) AS V(n) -- 10.000*10 = 100.000 filas (mรกx)
)
INSERT INTO tabladestinocalendario (FechaClave, Fecha, Aรฑo, Mes, NombreMes, Trimestre, SemanaAรฑo)
SELECT
    CAST(FORMAT(DATEADD(DAY, Num, @FechaInicio), 'yyyyMMdd') AS INT) AS FechaClave,
    DATEADD(DAY, Num, @FechaInicio) AS Fecha,
    YEAR(DATEADD(DAY, Num, @FechaInicio)) AS Aรฑo,
    MONTH(DATEADD(DAY, Num, @FechaInicio)) AS Mes,
    DATENAME(MONTH, DATEADD(DAY, Num, @FechaInicio)) AS NombreMes,
    DATEPART(QUARTER, DATEADD(DAY, Num, @FechaInicio)) AS Trimestre,
    DATEPART(WEEK, DATEADD(DAY, Num, @FechaInicio)) AS SemanaAรฑo
FROM Numeros;


A mi me funciona de manera eficiente para crear la tabla calendario basandome en set-based

Helpful resources

Announcements
November Fabric Update Carousel

Fabric Monthly Update - November 2025

Check out the November 2025 Fabric update to learn about new features.

Fabric Data Days Carousel

Fabric Data Weeks

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (27)