Multiple parents tree (or digraph) implementation sql server 2005

前端 未结 3 1945
遥遥无期
遥遥无期 2021-01-03 11:37

I need to implement a multi-parented tree (or digraph) onto SQL Server 2005. I\'ve read several articles, but most of them uses single-parented trees with a unique root like

3条回答
  •  梦毁少年i
    2021-01-03 12:23

    If you dont want to do the inserts suggested by Ronald,this would do!.

    WITH CTE_MultiParent  (ID, ParentID) 
    AS 
    (
        SELECT ID, ParentID FROM #ObjectRelations
        WHERE ID NOT IN 
        (
            SELECT DISTINCT ParentID FROM #ObjectRelations
        )
        UNION ALL
        SELECT ObjR.ID, ObjR.ParentID FROM #ObjectRelations ObjR INNER JOIN CTE_MultiParent
        ON CTE_MultiParent.ParentID = ObjR.Id
    )
    
    SELECT DISTINCT * FROM CTE_MultiParent
    

提交回复
热议问题