SQl Server - Hierarchical Data

Posted by JMSA on Stack Overflow See other posts from Stack Overflow or by JMSA
Published on 2009-09-07T18:25:12Z Indexed on 2010/03/31 6:43 UTC
Read the original article Hit count: 262

Filed under:
|
|

I use SQL Server 2000.

Suppose I have two tables like the following:

Area
----------------------------------
ID| Name   | HierarchyLevel
----------------------------------
1 | World  |     1
2 | America|     2
3 | Europe |     2
4 | Africa |     2
5 | USA    |     3

and

AreaHierarchy
------------------------
ID | ParentID | ChildID
------------------------
 1 |   1      |    2
 2 |   1      |    3
 3 |   1      |    4
 4 |   2      |    5

where

AreaHierarchy.ParentID and AreaHierarchy.ChildID are FKs of Area.ID

How can I find the nth parent of USA?

Is it possible without looping?

Probably not.

© Stack Overflow or respective owner

Related posts about sql

Related posts about tsql