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 |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-10-29 : 08:27:08
|
Bill writes "Windows 2000 Server SP4/SQL Server 2000 SP4I have a table with:dataid intparentid intdirname varchar(248)Basically it is a list of directories with associated parent directories. I have been asked to write a query that lists the top directory first, then each of the top directory's direct child directories with each of the child directory's direct child directories under them and so on. Below is sample table information and what the result should look like. The number of child directories may increase. We currently have about 5 levels.I would appreciate any help/direction you gurus can give me! I am at a complete loss.Thanks,Bill-- Sample Table Datadataid, parentid,dirname4000, 1000, Contract5436, 4000, Task15695, 4000, Task25621, 4000, Task35840, 5436, MIS Weekly Report5928, 5436, CMS Progress Report6403, 5695, MISS Monthly Report-- The result set should look like this:ContractTask1MIS Weekly ReportCMS Progress ReportTask2MISS Monthly ReportTask3" |
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
|
nr
SQLTeam MVY
12543 Posts |
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-10-29 : 09:42:32
|
Or this (which looks about like Nigel's without the spacing)set nocount ondeclare @level intset @level = 1create table #tree (id int identity (1,1), dataid int, level int)insert #tree (dataid, level) select dataid, @level from dirtable where parentid = 0 -- or is nullWhile @@rowcount > 0begin set @level = @level + 1 insert #tree (dataid, level) select d.dataid, @level from dirtable d inner join #tree t on t.dataid = d.parentid where t.level = @level - 1 order by dirnameendSelect dirname from #tree tinner join dirtable d on t.id = t.dataidorder by iddrop table #tree I'm curious to know why you want this, because it does not seen very useful.--KenI want to die in my sleep like my grandfather, not screaming in terror like his passengers. |
|
|
|
|
|
|
|