读书人

挑战性有关问题怎么生成带线的树形结

发布时间: 2012-01-19 20:57:58 作者: rapoo

挑战性问题,如何生成带线的树形结构结果集??
我的库表:
Id Name     ParentID
0 中国 0
1 山东 0
2 广东 0
3 济南 1
4 青岛 1
5 广州 2
6 佛山 2
7 东莞 2
8 虎门 2
9 历下区 3
10 白云区 5

如何根据库表检索生成如下结构的结果集?

□┬中国
├┬山东
│├┬济南
││└─历下区
│└─青岛
└┬广东
├┬广州
│└─白云区
├─佛山
├─东莞
└─虎门

这个问题好像有点复杂哦,不是单纯的行缩进那么简单,还要加上双字节的制表符号组成树结构连线,偶想了半天不知该如何下手,只好偷个懒到这来请教一下有没有那位高人做过,给指点一下,谢谢。

[解决办法]
--参考,邹老大写的方法

--测试数据
DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
INSERT @t SELECT '001 ',NULL , '山东省 '
UNION ALL SELECT '002 ', '001 ', '烟台市 '
UNION ALL SELECT '004 ', '002 ', '招远市 '
UNION ALL SELECT '003 ', '001 ', '青岛市 '
UNION ALL SELECT '005 ',NULL , '四会市 '
UNION ALL SELECT '006 ', '005 ', '清远市 '
UNION ALL SELECT '007 ', '006 ', '小分市 '

--深度排序显示处理
--生成每个节点的编码累计(相同当单编号法的编码)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM @t
WHERE PID IS NULL
WHILE @@ROWCOUNT> 0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
FROM @t a,@t_Level b
WHERE a.PID=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
/*--结果
|--山东省
|--烟台市
|--招远市
|--青岛市
|--四会市
|--清远市
|--小分市
--*/

[解决办法]
如果仅仅是要结果的话.参考一下*---------------------------------------*\
| Subject: Web TreeView Class |


| Version: 1.0 |
| Author: 黄方荣【meizz】【梅花雪】 |
| FileName: MzTreeView.js |
| Created: 2004-10-18 |
| LastModified: 2005-03-10 |
| Download: http://www.meizz.com/Web/Download/MzTreeView10.rar |
| Explain: http://www.meizz.com/Web/Article.asp?id=436 |
| Demo: http://www.meizz.com/Web/Demo/MzTreeView10.htm |
| |
| You may use this code on your item |
| this entire copyright notice appears unchanged |
| and you clearly display a link to http://www.meizz.com/
[解决办法]
我上面的答案还要做点小修改,防止位置错乱不同位数字符串比较的错乱
create table tree(id int,Name varchar(10),ParentID int)
insert tree select 0, '中国 ' ,-1
union all select 1, '山东 ' ,0
union all select 2, '广东 ' ,0
union all select 3, '济南 ' ,1
union all select 4, '青岛 ' ,1
union all select 5, '广州 ' ,2
union all select 6, '佛山 ' ,2
union all select 7, '东莞 ' ,2
union all select 8, '虎门 ' ,2
union all select 9, '历下区 ' ,3
union all select 10, '白云区 ' ,5
union all select 21, '测试区 ' ,1
create table #temp(id int,Name varchar(100),ParentID int,
treecode varchar(1000),level int)
declare @level int
select @level=0
insert into #temp select id, '□ '+
case when exists(select 1 from tree where id <> a.id and parentid=a.id)
then '┬ ' else '─ ' end +name,parentid, '0 ',0
from tree a where parentid=-1
while @@rowcount> 0
begin
set @level=@level+1
insert into #temp select a.id, '  '
+replace(replace(substring(b.name,2,@level-1), '└ ', '  '), '├ ', '│ ')
+case when not exists(select 1 from tree where parentid=b.id and
convert(varchar,id)> convert(varchar,a.id)) then '└ ' else '├ ' end
+case when exists(select 1 from tree where id <> a.id and parentid=a.id)
then '┬ ' else '─ ' end
+a.name,
a.parentid,b.treecode+ '. '+convert(varchar,a.id),@level
from tree a inner join #temp b on
a.parentid=b.id and b.level=@level-1
end
select * from #temp order by treecode,id

drop table #temp
drop table tree

name
------------------------------
□┬中国
 ├┬山东
 │├─测试区
 │├┬济南
 ││└─历下区
 │└─青岛
 └┬广东
  ├┬广州
  │└─白云区
  ├─佛山
  ├─东莞
  └─虎门

读书人网 >SQL Server

热点推荐