Recursive CTE: Work around

Nandan Hegde 36,156 Reputation points MVP Volunteer Moderator
2020-07-28T04:33:47.407+00:00

Hello All,
We are in process on migrating from on Prem server o Azure Synapse.
We see that Synapse does not support Recursive CTE.
So is using loop the only option for it because looping is degrading the performance.

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,380 questions
{count} vote

1 answer

Sort by: Most helpful
  1. HimanshuSinha-msft 19,491 Reputation points Microsoft Employee Moderator
    2020-07-29T01:25:21.213+00:00

    Hello Nandan ,

    Thanks for the question and nice to be in touch again .

    If we know the number of levels, you can do this:

    IF OBJECT_ID(N'dbo.RecurseTest') IS NOT NULL DROP TABLE dbo.RecurseTest;  
    GO  
    CREATE TABLE dbo.RecurseTest (  
        Id BIGINT NOT NULL,  
        EmployeeName VARCHAR(255) NOT NULL,  
        ParentId BIGINT NULL  
    )  
    
    insert into dbo.RecurseTest values(1, 'Emp1', 0);  
    insert into dbo.RecurseTest values(2, 'Emp2', 1);  
    insert into dbo.RecurseTest values(3, 'Emp3', 1);  
    insert into dbo.RecurseTest values(4, 'Emp4', 2);  
    insert into dbo.RecurseTest values(5, 'Emp5', 2);  
    insert into dbo.RecurseTest values(6, 'Emp6', 3);  
    

    Please find the full query attached .

    14099-1.png

    Thanks Himanshu

    Please do consider to click on "Accept Answer" and "Up-vote" on the post that helps you, as it can be beneficial to other community members

    14060-sqlquery.txt

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.