I'm trying out solution of SQL Query for Parent Child Relationship with the queries:
with [CTE] as (
select [ParentId]
, [NodeId]
from [TheTable] c where c.[ParentId] = 1
union all
select [ParentId]
, [NodeId]
from [CTE] p, [TheTable] c where c.[ParentId] = p.[NodeId]
)
select * from [CTE]
the errors:
The multipart identifier "p.[NodeId]" could not be bound
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
Not sure what to do next.
I expect it to return child at all level of a parent category.
e.g. for nodeId = 1, it should return 3, 4, 5, 6.