参考我的贴子:树形数据的处理
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.5049707
http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.5049707
解决方案 »
- 各位大侠,请问哪里有Sql Server的视频教程?
- SQL2005数据库类型问题
- http://www.dev-club.com/计算机开发者社区好几天不能登陆,谁知道什么原因啊,焦急等待中。。。
- union 关联查询排序问题
- 数据库查询统计的问题
- SQL2005中CHAR类型字段更新中文后成乱码(#),英文和数字没有问题
- 全身分数相送,一个关于SQL存储过程的问题
- sqlserver2005:equal to 操作中的排序规则冲突问题
- 高分讨教"公交信息查询系统"数据库设计!!!(100分)
- 急需报表控件
- where 的问题
- 查询的时候, 默认的所有者是谁 ? 如何实现将文本文件存入img字段并读出?
CREATE TABLE [dbo].[地方管理] (
[地名] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[编号] [numeric](18, 0) NULL ,
[所属] [numeric](18, 0) NULL ,
编号是主键
如
000 中国 -1
001 湖北 000
002 湖南 000
003 北京 001
这样可表示树了。
可以在前面的编号上做些文章通过编号可以表述父子关系。
遍例时用递归
CREATE TABLE [dbo].[地方管理] (
[地名] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[编号] [numeric](18, 0) NULL ,
[上级地名] [numeric](18, 0) NULL ,
[直接管辖地名] [numeric](18, 0) NULL
) ON [PRIMARY]insert into 地方管理
select '中国',001,0,0
union all select '北京', 002,001,001
union all select '广东', 003,001,001
union all select '上海', 004,001,001
union all select '广州', 005,003,003
union all select '佛山', 006,003,001
union all select '天河区', 007,005,005
union all select '天河路', 008,007,001
union all select '白云区', 009,005,005
union all select '天河一路', 010,008,001
union all select '天河二路', 008,001,003
union all select '南海', 012,006,001
union all select '顺德', 013,006,006
union all select '大历', 014,012,001
union all select '桂城', 015,012,006
go--创建一个函数,得到指定编号的所有子编号
create function f_child(@编号 numeric(18,0))
returns @re table(编号 numeric(18,0),level int)
as
begin
declare @l int
set @l=0
insert @re values(@编号,@l)
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.编号,@l
from 地方管理 a join @re b on a.上级地名=b.编号
where b.level=@l-1
end
return
end
go--调用实现查询
select a.* from 地方管理 a join dbo.f_child(001) b on a.编号=b.编号
go--删除测试
drop table 地方管理
drop function f_child/*--测试结果(所影响的行数为 15 行)地名 编号 上级地名 直接管辖地名
------- ------ ------- ------------------
中国 1 0 0
北京 2 1 1
广东 3 1 1
上海 4 1 1
广州 5 3 3
佛山 6 3 1
天河区 7 5 5
天河路 8 7 1
天河路 8 7 1
白云区 9 5 5
天河一路 10 8 1
天河一路 10 8 1
天河二路 8 1 3
天河二路 8 1 3
南海 12 6 1
顺德 13 6 6
大历 14 12 1
桂城 15 12 6(所影响的行数为 18 行)
--*/
天河一路->天河路->天河区->广州->广东->中国
中国不是直接管辖"广州"与"天河区"的,
而中国是直接管辖"广东","天河路","天河一路"
所以
广东:中国的一级管辖地
天河路:中国的二级管辖地
天河一路:中国的三级管理地.对"下级地"是由
天河一路->天河路->天河区->广州->广东->中国
这一条地方链来体现的.现在我想得到的结果是
某一地方所有的一级,二级,三级,...管辖地.
这条地方链.以上代码只是得到
天河一路->天河路->天河区->广州->广东->中国
地方链
union all select '天河路', 008,007,001
union all select '天河二路', 008,001,003 --这里的编号与上面重复了,应该是:011吧?2.中国的一级管辖地为:
北京
广东
上海
佛山-->广东-->中国
因此,佛山为二级管辖地大历-->南海-->佛山-->广东-->中国
因此南海为三级管辖地,大历为四级管辖地天河一路-->天河路-->天河区-->广州-->广东-->中国
因此,天河路为四级,天河一路为五级管辖地
而天河区的直接管辖地名不是中国(001),因此不显示出来
所以,如果查询中国,应该得到如下结果:地名 编号 上级地名 直接管辖地名 编号 管辖地级别
-------- -------- -------- -------- --------- ---------
北京 2 1 1 2 1
广东 3 1 1 3 1
上海 4 1 1 4 1
佛山 6 3 1 6 2
天河路 8 7 1 8 4
天河一路 10 8 1 10 5
南海 12 6 1 12 3
大历 14 12 1 14 4
-------- -------- -------- -------- --------- ---------
北京 2 1 1 2 1
广东 3 1 1 3 1
上海 4 1 1 4 1
佛山 6 3 1 6 2
天河路 8 7 1 8 4
天河一路 10 8 1 10 5
南海 12 6 1 12 3
大历 14 12 1 14 4这不是要求的结果集啦
北京
广东
上海
这也没有错,佛山-->广东-->中国
因此,佛山为二级管辖地这里因为"广东"为一级管辖地,所以佛山才是二级管辖地
这里如果"广东"的"直接管辖地名"不是001,而佛山的"直接管辖地名"是001
那么这里佛山才是一级管辖地
天河一路->天河路->天河区->广州->广东->中国
如果按你的理解,
广东为一级管辖地,
广州为二级管辖地,
天河区为三级管辖地,
天河路为四级管辖地,
天河一路为五级管辖地,我所指的是,
广州与天河区的"直接管辖地名"都不是001
所以应该是
广东为一级管辖地,
天河路为二级管辖地,
天河一路为三级管辖地.
-------- -------- -------- -------- --------- ---------
北京 2 1 1 2 1
广东 3 1 1 3 1
上海 4 1 1 4 1
佛山 6 3 1 6 2
天河路 8 7 1 8 2
天河一路 10 8 1 10 3
南海 12 6 1 12 3
大历 14 12 1 14 4
create function f_level(@编号 int)
returns @re table(编号 numeric(18,0),level int)
as
begin
declare @l int
declare @tb table(编号 numeric(18,0),level int,l int,chk bit) set @l=0
insert @tb select 编号,case 直接管辖地名 when @编号 then 1 else 0 end
,@l,case 直接管辖地名 when @编号 then 1 else 0 end
from 地方管理 where 上级地名=@编号 while @@rowcount>0
begin
set @l=@l+1
insert @tb select a.编号,b.level+case 直接管辖地名 when @编号 then 1 else 0 end
,@l,case 直接管辖地名 when @编号 then 1 else 0 end
from 地方管理 a join @tb b on a.上级地名=b.编号
where b.l=@l-1
end
insert @re select 编号,level from @tb where chk=1
return
end
go--调用实现查询
select a.*,管辖地级别=b.level
from 地方管理 a
join dbo.f_level(1) b on a.编号=b.编号
go
CREATE TABLE [dbo].[地方管理] (
[地名] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[编号] [numeric](18, 0) NULL ,
[上级地名] [numeric](18, 0) NULL ,
[直接管辖地名] [numeric](18, 0) NULL
) ON [PRIMARY]insert into 地方管理
select '中国',001,0,0
union all select '北京', 002,001,001
union all select '广东', 003,001,001
union all select '上海', 004,001,001
union all select '广州', 005,003,003
union all select '佛山', 006,003,001
union all select '天河区', 007,005,005
union all select '天河路', 008,007,001
union all select '白云区', 009,005,005
union all select '天河一路', 010,008,001
union all select '天河二路', 011,001,003
union all select '南海', 012,006,001
union all select '顺德', 013,006,006
union all select '大历', 014,012,001
union all select '桂城', 015,012,006
go--创建一个函数,得到级别表
create function f_level(@编号 int)
returns @re table(编号 numeric(18,0),level int)
as
begin
declare @l int
declare @tb table(编号 numeric(18,0),level int,l int,chk bit) set @l=0
insert @tb select 编号,case 直接管辖地名 when @编号 then 1 else 0 end
,@l,case 直接管辖地名 when @编号 then 1 else 0 end
from 地方管理 where 上级地名=@编号 while @@rowcount>0
begin
set @l=@l+1
insert @tb select a.编号,b.level+case 直接管辖地名 when @编号 then 1 else 0 end
,@l,case 直接管辖地名 when @编号 then 1 else 0 end
from 地方管理 a join @tb b on a.上级地名=b.编号
where b.l=@l-1
end
insert @re select 编号,level from @tb where chk=1
return
end
go--调用实现查询
select a.*,管辖地级别=b.level
from 地方管理 a
join dbo.f_level(1) b on a.编号=b.编号
go--删除测试
drop table 地方管理
drop function f_level/*--测试结果
地名 编号 上级地名 直接管辖地名 管辖地级别
-------------- ------- --------- ------------ ----------
北京 2 1 1 1
广东 3 1 1 1
上海 4 1 1 1
佛山 6 3 1 2
天河路 8 7 1 2
天河一路 10 8 1 3
南海 12 6 1 3
大历 14 12 1 4(所影响的行数为 8 行)
--*/