
ID
ID | NAME | ID1 |
1 | 重要工作 | 0 |
2 | 通知 | 1 |
3 | 工作动态 | 1 |
4 | 动态 | 3 |
也就是上面的显示的1是父类 2,3是1子类而4又是3的子类 | ||
这样显示的时候怎么查询啊
希望能写一下谢谢
[此贴子已经被作者于2007-11-20 19:11:49编辑过]
ID | NAME | ID1 |
1 | 重要工作 | 0 |
2 | 通知 | 1 |
3 | 工作动态 | 1 |
4 | 动态 | 3 |
也就是上面的显示的1是父类 2,3是1子类而4又是3的子类 | ||
[此贴子已经被作者于2007-11-20 19:11:49编辑过]
//在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]
能否写 得简单一点哦怎么看得不是嘿明白
对存储过程好象不是很了解 谢谢