i've a Problem with the hierarchyID and UserRights for a Menu. I only want to give the User the Right for Level 4 for example and my QUery should automaticly select all Parent from the Level 4 Child. How to do this ?
Do you understand my Problem ? I simply want all Parents (ancestors) from a child.
Greets Manuel
I've been working a lot with HierarchyId lately and I came across this question looking for answers to a different question. I thought I'd throw this example in the mix as it accounts for a few things. First, you can get your conditional expression in there without a recursive CTE. Second, GetDescendantOf is inclusive so you don't need to check
t1.NodeId = t2.NodeId
(and I generally prefer joins to subqueries). Here's a full demo you can play with:Something like this avoides the CTE
lets assume you have this table:
and you populate it so it hold this data:
and now you want to get all the parents of CompanyId 20001, this is how I did it:
you can change the recursive part of the CTE and not filter the top most node of the tree.
hope this helps,
Oded