//在sqlserver里测试成功.
[CODE]declare @t table(ID int,Name nvarchar(50),ID1 int)
insert into @t values(1,N'重要工作',0)
insert into @t values(2,N'通知',1)
insert into @t values(3,N'工作动态',1)
insert into @t values(4,N'动态',3)
declare @t_Level table(ID int,Level int,Sort varchar(8000))
declare @Level int
set @Level=0
insert @t_Level select ID,@Level,cast(ID as varchar)
from @t
where ID1=0
while @@rowcount>0
begin
set @Level=@Level+1
insert @t_Level select a.ID,@Level,b.Sort+cast(a.ID as varchar)
from @t a,@t_Level b
where a.ID1=b.ID and b.Level=@Level-1
end
select space(b.Level*2)+'|--'+a.Name
from @t a,@t_Level b
where a.ID=b.ID
order by b.Sort
/*
|--重要工作
|--通知
|--工作动态
|--动态
(所影响的行数为 4 行)
*/[/CODE]