sql adjacency list子节点

时间:2013-08-24 03:49:53

标签: sql sql-server tsql

给定上下文:How to copy a node's children in an adjacent list

在邻接列表中,如何选择任何节点并让所有孩子接受?

例如,请参阅下面突出显示的节点。

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用CTE(About CTE):

DECLARE @SelectedNode INT = 103;
;WITH nodes AS
( 
    SELECT Node
    , ParentNode
    , GroupID
    , Depth
    FROM TableNodes WHERE Node = @SelectedNode
    UNION ALL
    SELECT tn.Node
    , tn.ParentNode
    , tn.GroupID
    , tn.Depth FROM TableNode tn 
    INNER JOIN nodes n ON n.Node = tn.ParentNode
)

SELECT * FROM nodes;