I have the following tree hierarchical table
GO DROP TABLE #tbl GO CREATE TABLE #tbl (Id int , ParentId int) INSERT INTO #tbl (Id, ParentId) VALUES (0, NULL) INSERT INTO #tbl (Id, ParentId) VALUES (1, 0) INSERT INTO #tbl (Id, ParentId) VALUES (2, 1) INSERT INTO #tbl (Id, ParentId) VALUES (3, 1) INSERT INTO #tbl (Id, ParentId) VALUES (4, 2) INSERT INTO #tbl (Id, ParentId) VALUES (5, 2) INSERT INTO #tbl (Id, ParentId) VALUES (6, 3) INSERT INTO #tbl (Id, ParentId) VALUES (7, 3) GO
Which corresponds to the following tree
0 +- 1 +- 2 +- 4 +- 5 +- 3 +- 6 +- 7
Using the CTE recursive table, how can I get the path, as well as all the children of the selected node. For example, having 2 as input, how can I get the following data (if possible, arrange)
Id, ParentID ------- 0, NULL 1, 0 2, 1 4, 2 5, 2
I know that I can walk through the tree (get the path) with the following instruction
WITH RecursiveTree AS (
And with the following statement, cross in the tree (get all the children)
WITH RecursiveTree AS (
Question: How to combine these two teams into one?