I am trying to accomplish what, in my opinion, is complex recursion using CTE, is SQL Server 2008. I cannot wrap my head around this.
In the examples below, you can take a fixed depth of 3 ... nothing will ever be lower than that. In real life, the depth is "deeper", but still fixed. In this example, I tried to simplify it.
My input is similar to below.
ID PARENT_ID NAME DEPTH
The output of my CTE should be the following table.
LEVEL1_ID LEVEL2_ID LEVEL3_ID LEVEL1_NAME LEVEL2_NAME LEVEL3_NAME -------------------------------------------------------------------------------- 1 NULL NULL A NULL NULL 1 2 NULL AB NULL 1 2 3 ABC 1 4 NULL AD NULL
If I can get the identifier columns as a result of the output, I can of course match the names in the lookup table.
I am open to other ways to solve this problem, including using SSIS.
sql sql-server-2008 common-table-expression
thomas
source share