heirarchial data from self referencing table in tree form

Posted by Beta033 on Stack Overflow See other posts from Stack Overflow or by Beta033
Published on 2010-05-12T21:33:22Z Indexed on 2010/05/12 21:54 UTC
Read the original article Hit count: 227

Filed under:
|

Ii looks like this has been asked and answered in all the simple cases, excluding the one that i'm having trouble with. I've tried using a recursive CTE to generate this, however maybe a cursor would be better? or maybe a set of recursive functions will do the trick?

Can this be done in a cte?

consider the following table

PrimaryKey   ParentKey  
1            NULL       
2            1       
3            6
4            7
5            2
6            1
7            NULL

should yield

PK
1
-2
--5
-6
--3
7
-4

where the number of - marks equal the depth, my primary difficulty is the ordering.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server