sql递归查询问题
来源:互联网 发布:网络歌曲的动画mv 编辑:程序博客网 时间:2024/05/16 13:07
id upperid
1 2
3 2
4 1
5 3
比如说 upperid =2
那么先找到1,3,然后再由1,3找到4,5
insert into t
select 1, 2
union all select 3, 2
union all select 4, 1
union all select 5, 3
select * from t
create function aa(@upperid int)
returns @t table (id int,upperid int,level int)
as
begin
declare @i int
set @i=1
insert into @t
select *,@i from t where upperid=@upperid
while @@rowcount>0
begin
set @i=@i+1
insert into @t
select a.*,@i from t a left join @t b on a.upperid=b.id
where b.level=@i-1
end
return
end
----------- ----------- -----------
4 1 1
----------- ----------- -----------
1 2 1
3 2 1
4 1 2
5 3 2
这个需要level这个数,否则得不到.
if object_id('tbTest') is not null
drop table tbTest
if object_id('spGetChildren') is not null
drop proc spGetChildren
GO
create table tbTest(id int, upperid int)
insert tbTest
select 1, 2 union all
select 3, 2 union all
select 4, 1 union all
select 5, 3
GO
----创建存储过程
create proc spGetChildren @id int
as
declare @t table(id int)
insert @t select id from tbTest where upperid = @id
while @@rowcount > 0
insert @t select a.id from tbTest as a inner join @t as b
on a.upperid = b.id and a.id not in(select id from @t)
select * from @t
GO
declare @upperid int
set @upperid = 2
EXEC spGetChildren @upperid
drop proc spGetChildren
drop table tbTest
id
-----------
1
3
4
5
*/
这个就符合我的要求了.
returns @t table(id varchar(20))
as
begin
insert @t select wayid from tb where upperwayid = @id
while @@rowcount > 0
insert @t select a.wayid from tb as a inner join @t as b
on a.upperwayid = b.id and a.wayid not in(select id from @t)
return
end
表结构是这样的
部门 上级部门
A B
B C
C D
A A
B B
C C
求一条SQL语句,根据A查其上级部门,查询结果为
上级部门
B
C
D
=================================================
用函数
create table tb (部门 varchar(20),上级部门 varchar(20))
insert into tb select 'A','B' union all select 'B','C' union all select 'C','D'
union all select 'A','A' union all select 'B','B' union all select 'C','C'
--select * from tb
create function test_f (@name varchar(20))
returns @ta table(上级部门 varchar(20))
as
begin
--select @name=上级部门 from tb where 部门=@name and 部门!=上级部门
while exists(select 1 from tb where 部门=@name and 部门!=上级部门)
begin
insert @ta select 上级部门 from tb where 部门=@name and 部门!=上级部门
select @name=上级部门 from tb where 部门=@name and 部门!=上级部门
end
return
end
select * from dbo.test_f('A')
删除:
drop function test_f
drop table tb
上级部门
--------------------
B
C
D
(所影响的行数为 3 行)
(转自:http://blog.csdn.net/jackeyabc/archive/2007/03/19/1533775.aspx)
但是可以从部门到上级部门,却不知道怎么修改成为从上级部门到部门.所以最终没有采用
- sql递归查询问题
- sql递归查询问题
- sql递归查询问题
- sql递归查询问题
- sql递归查询问题
- SQL 递归查询
- SQL 递归查询
- SQL递归查询
- sql 递归查询
- SQL Server 递归查询
- SQL递归查询
- SQL递归查询函数
- SQL递归查询数据
- Sql 递归查询
- SQL递归查询函数
- SQL递归查询部门
- Sql 2005 递归查询
- SQL递归查询数据
- 【Java源码】TCP协议之服务端与客户端文件传输
- python 高级编程 四
- error LNK2019: 无法解析的外部符号 "public: virtual __thiscall oracle::occi::SQLException
- JavaScript 开发者经常忽略或误用的七个基础知识点
- MyBatis一对多和多对一
- sql递归查询问题
- Linux 系统内核空间与用户空间通信的实现与分析
- Windows 下进行 OCCI ( Oracle C++ Call Interface ) 开发
- poj 1038 - Bugs Integrated, Inc.(状态压缩dp)
- 用Uploadify上传文件,Uploadify3.2.1
- android service和activity获取屏幕尺寸
- Linux 下进行 OCCI ( Oracle C++ Call Interface ) 开发
- Android面试题:线程
- C++ 中 string 和其它类型的相互转化。