关于递归取记录的死循环问题

冥王星 2009-12-05 05:00:48
数据库中有个部门级别表administrative_region
regionint是本部门标识号,通过dbo.f_getparentint(regionint)能得到上级部门的标识号,region是名称
现在想通过一个给定的regionint得到它所有的上级名称直到顶层:

给定2048,得到:
总部浙江大区杭州分部杭州江干区办事处A

这些是分几条记录存在administrative_region 表中
regionint region
1 总部
256 浙江大区
512 杭州分部
1024 杭州江干区
2048 办事处A

以下是我在查询分析器中写的功能草稿,但是计算到“杭州江干区”级别就取不出上级的ID了,只能取出本单位ID,造成死循环
declare @regioncode nvarchar(30),@finalregion nvarchar(100),@curregionNum bigint,@regionNum bigint,@tmp bigint
set @curregionNum=16843015
set @regioncode=N'china'
while(cast(@curregionNum as bigint)>5)
begin

select @regioncode=region,@regionNum=regionint from administrative_region where regionint=@curregionNum
set @tmp=dbo.f_getparentInt(@curregionNum)
set @curregionNum=@tmp
print @curregionNum
set @finalregion=cast(@regioncode as nvarchar)+cast(@finalregion as nvarchar)

end
select @finalregion
...全文
212 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
冥王星 2009-12-07
  • 打赏
  • 举报
回复
谢谢,解决了,
是我的取上级区域的函数写错了。

CREATE   FUNCTION   [dbo].[f_getParentCode](   
@regionCode nvarchar(15)
)RETURNS nvarchar(15)
AS
BEGIN
DECLARE @tmpStr nvarchar(20)

SET @tmpstr=reverse(@regionCode)
while (left(@tmpstr,2)='0.')
begin
set @tmpstr=stuff(@tmpstr,1,2,'')
end
set @tmpstr=stuff(@tmpstr,1,charindex('.',@tmpstr),'')
if charindex('.',@regionCode )>0
set @tmpstr=reverse(@tmpstr)
else
set @tmpstr=N''
RETURN(@tmpstr)
END




declare @regioncode nvarchar(30),@region varchar(20),@finalregion nvarchar(100),@curregionNum bigint,@regionNum bigint,@tmp bigint ,@subregionNum bigint,@tmp1 bigint,@tmp2 bigint ,@tmp3 bigint ,@tmp4 bigint
set @subregionNum =16843015
set @tmp1=0
set @finalregion=N''--@finalregion 必须要有个初始值,不然最终值会变成NULL
while(@tmp1<2)
begin
select @region=region,@curregionNum=regionint ,@regioncode=region_code from administrative_region where regionint=@subregionNum
set @finalregion=cast(@region as nvarchar)+@finalregion
set @regioncode=dbo.f_getparentcode(@regioncode)
if charindex('.',@regioncode)=0 --是否最高级别
begin
set @tmp1=@tmp1+1
end
set @subregionNum=dbo.f_code2int(@regioncode)

end

最终结果是:
总部浙江大区杭州分部杭州江干区办事处A (一条记录累加上级的名称直到最高级,并非是多个记录)
ChinaJiaBing 2009-12-05
  • 打赏
  • 举报
回复

--测试数据
CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
INSERT tb 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','小分市'
GO

--查询指定节点及其所有子节点的函数
--2000
CREATE FUNCTION f_Cid(@PID char(3))
RETURNS @t_Level TABLE(PID char(3),Level int)
AS
BEGIN
DECLARE @Level int
SET @Level=1
INSERT @t_Level SELECT @PID,@Level
WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.PID,@Level
FROM tb a,@t_Level b
WHERE a.ID=b.PID
AND b.Level=@Level-1
END
RETURN
END
GO

--调用函数查询002及其所有子节点
SELECT a.*
FROM tb a,f_Cid('002') b
WHERE a.ID=b.PID
go
--2005
;With china as
(
select *,lev=1 from tb where id='002'
union all
select b.*,lev=a.lev+1 from china a ,tb b where a.pid=b.id
)
select * from china
go
drop table tb
go
drop function f_Cid


/*

(7 行受影响)
ID PID Name
---- ---- ----------
001 NULL 山东省
002 001 烟台市

(2 行受影响)

ID PID Name lev
---- ---- ---------- -----------
002 001 烟台市 1
001 NULL 山东省 2

(2 行受影响)




*/



--小F-- 2009-12-05
  • 打赏
  • 举报
回复
答案石头写出来了 不过你可以看看这个
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-09-30 08:52:38
set nocount on
if object_id('tb','U')is not null drop table tb
go
create table tb(ID int, ParentID int)
insert into tb select 1,0
insert into tb select 2,1
insert into tb select 3,1
insert into tb select 4,2
insert into tb select 5,3
insert into tb select 6,5
insert into tb select 7,6
-->Title:查找指定節點下的子結點
if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID
go
create function Uf_GetChildID(@ParentID int)
returns @t table(ID int)
as
begin
insert @t select ID from tb where ParentID=@ParentID
while @@rowcount<>0
begin
insert @t select a.ID from tb a inner join @t b
on a.ParentID=b.id and
not exists(select 1 from @t where id=a.id)
end
return
end
go
select * from dbo.Uf_GetChildID(5)
/*
ID
-----------
6
7
*/
-->Title:查找指定節點的所有父結點
if object_id('Uf_GetParentID')is not null drop function Uf_GetParentID
go
create function Uf_GetParentID(@ID int)
returns @t table(ParentID int)
as
begin
insert @t select ParentID from tb where ID=@ID
while @@rowcount!=0
begin
insert @t select a.ParentID from tb a inner join @t b
on a.id=b.ParentID and
not exists(select 1 from @t where ParentID=a.ParentID)
end
return
end
go
select * from dbo.Uf_GetParentID(2)
/*
ParentID
-----------
1
0
*/
-狙击手- 2009-12-05
  • 打赏
  • 举报
回复

declare @regioncode nvarchar(30),@finalregion nvarchar(100),@curregionNum bigint,@regionNum bigint,@tmp bigint
set @curregionNum=2048
set @regioncode=N'china'
set @finalregion = ''
while(cast(@curregionNum as bigint)>5)
begin

select @regioncode=region,@regionNum=regionint
from administrative_region
where regionint=@curregionNum
select top 1 @tmp = regionint from administrative_region
where regionint < @regionNum
order by regionint desc

set @curregionNum=@tmp
print @curregionNum
set @finalregion=cast(@regioncode as nvarchar)+cast(@finalregion as nvarchar)

end
select @finalregion

--Result:
/*

-------------------------------------------------
浙江大区杭州分部杭州江干区办事处A

(1 行受影响)
*/
--End
-狙击手- 2009-12-05
  • 打赏
  • 举报
回复

declare @regioncode nvarchar(30),@finalregion nvarchar(100),@curregionNum bigint,@regionNum bigint,@tmp bigint
set @curregionNum=2048
set @regioncode=N'china'
set @finalregion = ''
while(cast(@curregionNum as bigint)>5)
begin

select @regioncode=region,@regionNum=regionint
from administrative_region
where regionint=@curregionNum
select top 1 @tmp = regionint from administrative_region
where regionint < @regionNum
order by regionint desc

set @curregionNum=@tmp
print @curregionNum
set @finalregion=cast(@regioncode as nvarchar)+cast(@finalregion as nvarchar)

end
select @finalregion

-狙击手- 2009-12-05
  • 打赏
  • 举报
回复
树哥
sgtzzc 2009-12-05
  • 打赏
  • 举报
回复
/*
标题:查询指定节点及其所有父节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有父节点的函数
create function f_pid(@id varchar(3)) returns @t_level table(id varchar(3))
as
begin
insert into @t_level select @id
select @id = pid from tb where id = @id and pid is not null
while @@ROWCOUNT > 0
begin
insert into @t_level select @id select @id = pid from tb where id = @id and pid is not null
end
return
end
go

--调用函数查询002(广州市)及其所有父节点
select a.* from tb a , f_pid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有父节点
select a.* from tb a , f_pid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市

(所影响的行数为 2 行)
*/

--调用函数查询008(西乡镇)及其所有父节点
select a.* from tb a , f_pid('008') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
003 001 深圳市
007 003 宝安区
008 007 西乡镇

(所影响的行数为 4 行)
*/

drop table tb
drop function f_pid
sgtzzc 2009-12-05
  • 打赏
  • 举报
回复
/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--查询指定节点及其所有子节点的函数
create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.pid = b.id and b.level = @level - 1
end
return
end
go

--调用函数查询001(广东省)及其所有子节点
select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
001 NULL 广东省
002 001 广州市
003 001 深圳市
004 002 天河区
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 10 行)
*/

--调用函数查询002(广州市)及其所有子节点
select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
002 001 广州市
004 002 天河区

(所影响的行数为 2 行)
*/

--调用函数查询003(深圳市)及其所有子节点
select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
/*
id pid name
---- ---- ----------
003 001 深圳市
005 003 罗湖区
006 003 福田区
007 003 宝安区
008 007 西乡镇
009 007 龙华镇
010 007 松岗镇

(所影响的行数为 7 行)
*/

drop table tb
drop function f_cid

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧