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 |
|
Cousboy
Starting Member
7 Posts |
Posted - 2011-01-17 : 17:22:27
|
| Hi, I'm trying to extract a project hierarchy from from an adjacency model table in SQL Sever 2008.I have created a table which when I enter the statement:SELECT * FROM prproject ORDER BY project ASCI get these results:Project---Parent1---------NULL1.1-------11.1.1-----1.11.1.2-----1.11.1.3-----1.12---------NULL2.1-------22.1.1-----2.12.1.2-----2.12.1.3-----2.1What I would like to do is write a select statement or stored procedure that when I specify a "Parent" project all of the hierarchy to the nth level is shown in the results. For example if I selected parent project "1" then the results would be displayed as:Project1.11.1.11.1.21.1.3Any help will be greatly appreciated.Many thanksCousboy |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2011-01-18 : 00:48:44
|
| [code];WITH cte(project)AS( SELECT Project FROM prproject WHERE Project = '1' UNION ALL SELECT s.Project FROM cte c JOIN prproject s ON c.project = s.Parent )SELECT * FROM cte WHERE project <> '1'[/code] |
 |
|
|
|
|
|