Solved

Hierarchical SQL table and finding a record's lineage

Posted on 2010-11-29
3
653 Views
Last Modified: 2012-05-10
Hello.

I have a hierarchical SQL table mimicking a directory tree structure, with fields like the following:

ID
ParentID
ItemName

where a record's ParentID is defined as the ID of the record's intended parent record.

When I preform a Select, I would like to add a column, named "Path", that contains a string representation of an item's lineage, (Eg \GrandParent\Parent\Me).  Given the ID of any item within the table, how do I design a select command to recursively build a lineage string?  I use the word "recursion" loosely -- I know that actual recursion is a resource hog.  Does anyone have a workable strategy for this?

Something like:
SELECT ID, ParentID, ItemName (...something...) as Path
FROM MyTable
WHERE ID=@ID
0
Comment
Question by:ewolsing
3 Comments
 
LVL 18

Expert Comment

by:lludden
Comment Utility
If you are using SQL 2005 or later, you might try a recursive CTE as opposed to a recursive function.

Check this MSDN article.

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

Accepted Solution

by:
cyberkiwi earned 500 total points
Comment Utility
A sample
create table tree (ID int, ParentID int, ItemName varchar(100))

insert tree select

1,null,'GrandParent' union all select

2,1,'Parent' union all select

3,2,'Me'

GO



create function dbo.lineage(@id int)

returns varchar(max)

as

begin

declare @path varchar(max);

with tmp as (

	select level=1, parentId,convert(varchar(max),'\'+ItemName) Path

	from tree where id=@id

	union all

	select level+1,t.parentID,'\'+t.ItemName+tmp.path

	from tmp

	inner join tree t on tmp.parentID=t.id

	)

select top(1) @path=path from tmp order by level desc

return @path

end

GO



select id, parentid, dbo.lineage(id) as path

from tree

where id=3

Open in new window

0
 

Author Closing Comment

by:ewolsing
Comment Utility
Thank you!  That was exactly what I was looking for.  While I've done a fair amount of SQL work, I've never played with recursion before.  I appreciate the insight.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
t-sql complement 8 29
How to find all values in a column which is not money 25 24
SQL Server 2012 Row Selection 2 29
SQL JOIN 6 31
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now