Quantcast
Channel: Questions in topic: "cte"
Viewing all articles
Browse latest Browse all 75

How do I sequence number a recursive CTE top to bottom?

$
0
0
I have a recursive cte which I need to turn in to a flat file recordset AND MAINTAIN THE ORDER TOP TO BOTTOM AND KEEP TRACK OF WHICH ITEMS REPORT TO EACH LEVLEL BY THE ORDER NUMBER. Data Item Parent A null B null C A D A E C F C G F H G recursing the data above Should return Index Parent Item 1 0 A 2 0 B 3 1 C 4 1 D 5 3 E 6 3 F 7 6 G 8 7 H This should not be that tough but I can't figure it out. Row_number() resets at each level of the recursion. I tried a function accessing a table of numbers but function won't accept cursor arguments or dynamic sql so it would only work for 1 user at a time which is unacceptable. I actually made it work using the format below and indenting 3 decimal places at each level but it is limited to 999 items in each recursion and only about 11 levels of recursion plus it is just an ugly hack. Index Parent 1.10000000000000000000000000000000000 1.00000000000000000000000000000000000 1.10100000000000000000000000000000000 1.10000000000000000000000000000000000 1.10100100000000000000000000000000000 1.10100000000000000000000000000000000 1.10100100100000000000000000000000000 1.10100100000000000000000000000000000 1.10100100200000000000000000000000000 1.10100100000000000000000000000000000 1.10100100300000000000000000000000000 1.10100100000000000000000000000000000 1.10100100400000000000000000000000000 1.10100100000000000000000000000000000 1.10100100400100000000000000000000000 1.10100100400000000000000000000000000 Global or static variables would make this easy.c'mon Microsoft. Any ideas greatly appreciated.

Viewing all articles
Browse latest Browse all 75

Trending Articles