I have a table with the following structure
ID Name Parent
----------- -------------------------------------------------- -----------
1 Root NULL
2 Root_A 1
3 Root_B 1
4 Root_C 1
5 Root_C_A 4
6 Root_C_A_A 5
7 Root_C_A_A_A 6
So if I pass 7, I would like to get the following
Root --> Root_C --> Root_C_A --> Root_C_A_A
That means I want to traverse back to root from a child. How can I do it using SQL Server 2008 Stored Procedures or with other .Net techniques ?
I think I can accomplish the task using recursive function
You can implement recursion in Sql Server 2005+ using a common table expression. CTEs let you join to themselves in order to recurse. The CTE continues to recurse until no rows are returned, so you’ll want to ensure you reliably meet that end condition.