Similar to this question: How do I query for all the nodes between two nodes in a tree?
But I do not have a closure (flattened) table, a child can have many parents, and ID traversal is not necessarily in order. There is no limit to the nesting depth.
Assume a circular reference is impossible... I would like to return all rows required to traverse the hierarchy.
Assume the following table:
ParentID ID RowNumber(Reference)
1 2 1
2 4 2
4 3 3
3 5 4
1 6 5
6 7 6
2 8 7
3 9 8
1 8 9
6 8 10
Given 1
how would I write a single query to return all the rows (get all descendants' relationsips)?
Likewise, given 2
I would expect rows 2,3,4,7,8
Given 6
I would expect rows 6 and 10
An occasional false positive is acceptable as are duplicated rows in the result. A missing row is unacceptable
Implementing in MSAccess and SQL Server 2000+