Hi I am trying to get the full hierarchy of my category. Here is my sample table
ID PARENT_ID NAME DEPTH
------------------------------------------
1 NULL A 1
2 NULL B 1
3 NULL C 1
4 1 D 2
5 4 E 3
The cte output should be this
ID PARENT_ID NAME
---------------------------
1 NULL A
2 NULL B
3 NULL C
4 1 D
5 4 E
5 1 E
As you can see id:5 is parent of 4 and 1. How do i query the whole tree
In SQL Server 2008 CTE can be use to query recursively.
Example of CTE from MSDN
-- Sample Solution (Untested)