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.
Author |
Topic |
rizwanmgm
Starting Member
8 Posts |
Posted - 2012-05-01 : 06:48:02
|
dear friends,i need your help.i have one table and fields areitemid name parentid1 Root null2 IT 13 sales 14 report 25 doc 4now how can i query to find name doc and it should return me complete path like doc is under Root/it/report.means on result i want to show the parentid as complete path.i hope you understand.Thanks |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-05-01 : 07:14:01
|
If you want to work frequently with hierarchies, you might want to see if the hierarchyid feature available in SQL 2008 would be of use to you. http://msdn.microsoft.com/en-us/magazine/cc794278.aspx If you just want to get the hierarchy tree as a one-time thing, you can use a recursive CTE like this:;WITH cte AS( SELECT *,CAST('' AS VARCHAR(MAX)) AS [Path] FROM YourTable WHERE parentId IS NULL UNION ALL SELECT y.*, c.Path+'/'+c.NAME FROM cte c INNER JOIN YourTable y ON y.parentId = c.ItemId)SELECT * FROM cte; |
 |
|
|
|
|