Alternative manner to implement an hierarchy without using recursive CTEs

pmscorca 1,052 Reputation points
2025-12-16T06:31:25.8+00:00

Hi,

I'm searching an alternative manner to implement an hierarchy in T-SQL without using recursive CTEs. Actually, a MS Fabric warehouse doesn't support a such useful feature and I need to use recursive CTEs in some scenarios.

Any suggests to me, please? Many thanks

SQL Server | SQL Server Transact-SQL
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2025-12-16T06:31:35.09+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    One alternative manner to implement a hierarchy in T-SQL without using recursive CTEs is to utilize the parent/child approach. In this method, each row in your table contains a reference to its parent, allowing you to establish a hierarchical relationship without recursion. Here’s an example of how you can set this up:

    CREATE TABLE ParentChildOrg (
        BusinessEntityID INT PRIMARY KEY,
        ManagerId INT FOREIGN KEY REFERENCES ParentChildOrg(BusinessEntityID),
        EmployeeName NVARCHAR(50)
    );
    GO
    

    In this example, the ManagerId column references the BusinessEntityID of the parent, creating a direct link between each employee and their manager. This structure allows for efficient querying of the hierarchy without the need for recursive CTEs, especially when the hierarchy does not change frequently.

    Additionally, consider using XML data types if your application requires storing and retrieving the complete hierarchy in XML format. This can be beneficial when the data is consumed in XML format and predicate searches are limited.


    References:

    0 comments No comments

Your answer

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