create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))insert tb
select '0001',null,'云南省'
union all select '0002','0001','昆明市'
union all select '0003','0001','昭通市'
union all select '0009','0001','大理市'
union all select '0008',null,'四川省'
union all select '0004',null,'贵州省'
union all select '0005','0002','五华区'
union all select '0007','0002','水富县'
union all select '0006','0005','西园路192号'
union all select '0010','0006','金色梧桐3-702'
union all select '0011','0010','昆明越科时空科技有限公司'
union all select '0015','0007','两碗乡'
union all select '0013','0015','两碗村'
union all select '0012','0013','某跨国集团董事长'
union all select '0014','0008','成都市'select * from tb
--创建树
--@level_tt 表变量,相当于表
declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),levela int)
declare @level int
set @level=0
insert into @level_tt(ybh,ebh,levela)
select ybh,ybh,@level from tb where ebh is null
--select * from @level_tt
while @@ROWCOUNT>0
begin
set @level=@level+1
insert into @level_tt(ybh,ebh,levela)
(select a.ybh,b.ebh+a.ybh,@level
from tb a inner join @level_tt b
on a.ebh=b.ybh and b.levela=@level-1)
end
--select * from @level_tt order by ebh asc
select space(b.levela*2)+'----'+a.beizhu as ss,a.*,b.*
from tb a inner join @level_tt b
on a.ybh=b.ybh
order by b.ebh
查询显示为:
那如何在页面上显示成树呢?
你写这么多,对于客户端来说一点用不上
在asp.net里面对DataSet递归就行了
http://study.iader.com/ASP.NET/h001/h02/1152158505d102610.html
这个是在数据库里面用的,asp.net不需要这么复杂,开发语言递归很容易就完成了
http://topic.csdn.net/u/20090708/15/6404e1d3-5807-4249-a694-185cda4b6489.html
这个帖子上有用到树,一样的做法
这代码不全啊DataSet ds=bindds();//获取DataSet,具体代码略 --没这方法