Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Heirarchy question...

Author  Topic 

HalaszJ
Yak Posting Veteran

59 Posts

Posted - 2010-03-18 : 21:52:44
I have a table of organizations, each have an organization_name, organization_id, and a organization_parent_id. I want to find out the name of all the parents, it's not as simple as just 1 level up, if there are great grand children, I want to get the name of the paent, not the grand children... example

root
-parent
--child
---grand child
----great grand child
-parent
--child
---grand child
-parent
--child
-parent
--child
---grand child

for all the above, i want to get the value of parent, so if i pass in the organization_id of the great grand child, i just want the parent... is this possible without a lot of work or overhead on the server?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-19 : 12:29:23
its possible using recursive ctes, see below

http://msdn.microsoft.com/en-us/library/ms186243.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -