Mike,
[quoted text, click to view] > I've spent alot of time trying but can't get the following
> statement to select the hierarchy results into a temporary table
> so that I may base a report on it.
create table Hierarchy (
Parent varchar(30) not null,
Child varchar(30) not null)
go
insert Hierarchy (Parent, Child) values ('World', 'Europe')
insert Hierarchy (Parent, Child) values ('World', 'North
America')
insert Hierarchy (Parent, Child) values ('Europe', 'France')
insert Hierarchy (Parent, Child) values ('France', 'Paris')
insert Hierarchy (Parent, Child) values ('North America', 'United
States')
insert Hierarchy (Parent, Child) values ('North America', 'Canada')
insert Hierarchy (Parent, Child) values ('United States', 'New
York')
insert Hierarchy (Parent, Child) values ('United States',
'Washington')
insert Hierarchy (Parent, Child) values ('New York', 'New York
City')
insert Hierarchy (Parent, Child) values ('Washington', 'Redmond')
go
create procedure expand (@current char(20)) as
set nocount on
declare @Level int
declare @Count int
create table #Tree (
LevelNbr int not null,
Parent varchar(20) not null,
Child varchar(20) not null,
SortSeq varchar(4000) not null
)
insert #Tree
select distinct 0, Parent, Parent, Parent
from Hierarchy
where Parent = @Current
select @Count = @@rowcount, @Level = 0
while (@Count > 0) begin
insert #Tree
select @Level + 1,
Hierarchy.Parent,
Hierarchy.Child,
#Tree.SortSeq + '.' + Hierarchy.Child
from Hierarchy
join #Tree
on #Tree.Child = Hierarchy.Parent
where #Tree.LevelNbr = @Level
select @Count = @@rowcount, @Level = @Level + 1
-- select @Count, @Level
end
select replicate (' ', Levelnbr) + Child from #Tree
order by SortSeq
drop table #Tree
go
exec expand 'World'
exec expand 'North America'
go
drop table Hierarchy
drop procedure expand