Sql Server CTE Parent Child recursive
问题 I have following table structure: create table Test( ParentId int, ChildId int ) insert into Test(ParentId, ChildId) select 1, NULL union select 1, 2 union select 1, 3 union select 2, NULL union select 2, 5 union select 5, 6 union select 6, 5 union select 6, 8 I'm trying to build a result set of all parent child DIRECT and INDIRECT relationships . So suppose I pass the parameter of ParentID = 2, I would like the result set to return exactly as below: ParentId ChildId ------------------- 2