SQL的递归查询

mzcih 2008-04-09 04:32:46
递归查询

AAA表

字段
ID(自动编号) strNum1(主编号)strNum2(次编号) intTaxis(顺序)
1 01 001 1
2 001 011 2
3 001 010 1
6 01 002 2
4 002 021 1
5 002 022 2
......


搜索出来的结果如下:

当查询 01 时结果如下

01(主层)(包含001、002)
001(次层)(包含011、010)
010(次次层)(按顺序排列 1)
011(次次层)(按顺序排列 2)
002(次层)(包含021、022)
021(次次层)(按顺序排列 1)
022(次次层)(按顺序排列 2)
......

当查询 001 时结果如下
001(主层)(包含011、010)
010(次层)(按顺序排列 1)
011(次层)(按顺序排列 2)

请教大家有什么简洁的好方法查询出来呢?
...全文
8348 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
越过越咸 2011-07-23
  • 打赏
  • 举报
回复
冬雨在路上 2010-12-20
  • 打赏
  • 举报
回复
学习了
ycagri 2010-09-06
  • 打赏
  • 举报
回复
这个是要支持一下的
chengkairen2 2009-11-21
  • 打赏
  • 举报
回复
学习看看
newqq 2008-04-11
  • 打赏
  • 举报
回复
:)
mzcih 2008-04-11
  • 打赏
  • 举报
回复
谢谢大家的帮忙,结贴了。
正宗老冉 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 y_dong119 的回复:]
引用 8 楼 happyflystone 的回复:
引用 7 楼 ranzj 的回复:
钻石的高度,难以企及啊!
向 libin_ftsafe 学习!
[/Quote]
y_dong119 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 happyflystone 的回复:]
引用 7 楼 ranzj 的回复:
钻石的高度,难以企及啊!
向 libin_ftsafe 学习!

[/Quote]
-狙击手- 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ranzj 的回复:]
钻石的高度,难以企及啊!
向 libin_ftsafe 学习!
[/Quote]
正宗老冉 2008-04-09
  • 打赏
  • 举报
回复
钻石的高度,难以企及啊!
向 libin_ftsafe 学习!
子陌红尘 2008-04-09
  • 打赏
  • 举报
回复
针对楼主需求在SQL Server 2000环境下的实现:

create table AAA(ID INT,strNum1 varchar(8),strNum2 varchar(8),intTaxis int) 
insert into AAA select 1,'01' ,'001',1
insert into AAA select 2,'001','011',2
insert into AAA select 3,'001','010',1
insert into AAA select 6,'01' ,'002',2
insert into AAA select 4,'002','021',1
insert into AAA select 5,'002','022',2
go

--创建用户定义函数
create function f_getChild(@strNum1 VARCHAR(10))
returns @t table(strNum1 VARCHAR(10),strNum2 VARCHAR(10),Level INT,Ord varchar(100))
as
begin
declare @i int
set @i=1
insert into @t select strNum1,strNum2,@i,right('0'+strNum2,3) from AAA where strNum1 = @strNum1

while @@rowcount<>0
begin
set @i=@i+1
insert into @t
select
a.strNum1,a.strNum2,@i,b.Ord+a.strNum2
from
AAA a,@t b
where
a.strNum1=b.strNum2 and b.Level=@i-1
and
not exists(select 1 from @t where strNum1=a.strNum1 and strNum2=a.strNum2)
end
return
end
go

--执行查询
select * from dbo.f_getChild('01') order by Ord
go

--输出结果
/*
strNum1 strNum2 Level Ord
---------- ---------- ----------- -----------
01 001 1 001
001 010 2 001010
001 011 2 001011
01 002 1 002
002 021 2 002021
002 022 2 002022
*/

--删除测试数据
drop function f_getChild
drop table AAA
go
flairsky 2008-04-09
  • 打赏
  • 举报
回复
Declare @Id Int
Set @Id = 5; ---在此修改父节点

With RootNodeCTE(Id,ParentId)
As
(
Select Id,ParentId From BOM Where ParentId In (@Id)
Union All
Select BOM.Id,BOM.ParentId From RootNodeCTE
Inner Join BOM
On RootNodeCTE.Id = BOM.ParentId
)

Select * From RootNodeCTE

正解
mzcih 2008-04-09
  • 打赏
  • 举报
回复
谢谢,我测试下先,单看代码量比我之前写的简洁许多。
utpcb 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 libin_ftsafe 的回复:]
在SQL Server2005中其实提供了CTE[公共表表达式]来实现递归:

关于CTE的使用请查MSDN



SQL codeDeclare @Id Int
Set @Id = 5; ---在此修改父节点

With RootNodeCTE(Id,ParentId)
As
(
Select Id,ParentId From BOM Where ParentId In (@Id)
Union All
Select BOM.Id,BOM.ParentId From RootNodeCTE
Inner Join BOM
On RootNodeCTE.Id = BOM.ParentId
)

Select * From RootNodeCTE
[/Quote]
子陌红尘 2008-04-09
  • 打赏
  • 举报
回复
在SQL Server2005中其实提供了CTE[公共表表达式]来实现递归:

关于CTE的使用请查MSDN


Declare @Id Int 
Set @Id = 5; ---在此修改父节点

With RootNodeCTE(Id,ParentId)
As
(
Select Id,ParentId From BOM Where ParentId In (@Id)
Union All
Select BOM.Id,BOM.ParentId From RootNodeCTE
Inner Join BOM
On RootNodeCTE.Id = BOM.ParentId
)

Select * From RootNodeCTE
子陌红尘 2008-04-09
  • 打赏
  • 举报
回复
SQL Server 2000环境下参考:

--生成测试数据
create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
insert into BOM select 1,0,NULL
insert into BOM select 2,1,NULL
insert into BOM select 3,1,NULL
insert into BOM select 4,2,NULL
insert into BOM select 5,3,NULL
insert into BOM select 6,5,NULL
insert into BOM select 7,6,NULL
go

--创建用户定义函数
create function f_getChild(@ID VARCHAR(10))
returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
as
begin
declare @i int,@ret varchar(8000)
set @i = 1
insert into @t select ID,PID,@i from BOM where PID = @ID

while @@rowcount<>0
begin
set @i = @i + 1

insert into @t
select
a.ID,a.PID,@i
from
BOM a,@t b
where
a.PID=b.ID and b.Level = @i-1
end
return
end
go

--执行查询
select ID from dbo.f_getChild(3)
go

--输出结果
/*
5
6
7
*/

--删除测试数据
drop function f_getChild
drop table BOM

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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