【转】sql递归查询问题
原文链接地址http://www.cnblogs.com/sweting/archive/2009/06/08/1498483.html
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查询方法有一种叫递归查询,整理如下: 一.查询出 id = 1 的所有子结点 wit ...
- SQL递归查询(with as)
SQL递归查询(with cte as) with cte as( select Id,Pid,DeptName,0 as lvl from Department where Id = 2 ...
- 关于SQL递归查询在不同数据库中的实现方法
比如表结构数据如下: Table:Tree ID Name ParentId 1 一级 0 2 二级 1 3 三级 2 4 四级 3 SQL SERVER 2005查询方法: //上查 with ...
- SQL递归查询知多少
最近工作中遇到了一个问题,需要根据保存的流程数据,构建流程图.数据库中保持的流程数据是树形结构的,表结构及数据如下图: 仔细观察表结构,会发现其树形结构的特点: FFIRSTNODE:标记是否为根节点 ...
- sql递归查询语句
sql Bom 递归查询: with t as(select * from Department where id=6union allselect a.* from Department a,t w ...
- SQL递归查询实现组织机构树
系统用到的组织机构树,要实现对当前节点以及其子节点的查询,数据库SQL要用到递归查询,这也是我第一次接触SQL的递归查询. 先说一下什么是递归查询,简单说来是将一个树状结构存储在一张表里,比如一个表中 ...
- SQL 递归查询,意淫CTE递归的执行步骤
今天用到了sql的递归查询.递归查询是CTE语句with xx as(....)实现的. 假如表Category数据如下. 我们想查找机枪这个子分类极其层次关系(通过子节点,查询所有层级节点).以下是 ...
- easyUI 的tree 修改节点,sql递归查询
1.easyUI 的tree 修改节点: 我需要:切换语言状态,英文下, 修改根节点文本,显示英文. 操作位置:在tree的显示 $('#tree').tree(),onLoadSuccess事件方法 ...
- SQL递归查询(with cte as)
with cte as ( select Id,Pid,DeptName,0 as lvl from Department where Id = 2 union all select d.Id,d.P ...
随机推荐
- OCSP
一.简介 二.协议 三.其他 1)OCSP装订 https://zh.wikipedia.org/wiki/OCSP%E8%A3%85%E8%AE%A2
- js的RegExp
正则表达式是一种用来匹配字符串的强有力的武器.它的设计思想是用一种描述性的语言来给字符串定义一个规则,凡是符合规则的字符串,我们就认为它“匹配”了,否则,该字符串就是不合法的. 所以我们判断一个字符串 ...
- linux基础编程:IO模型:阻塞/非阻塞/IO复用 同步/异步 Select/Epoll/AIO(转载)
IO概念 Linux的内核将所有外部设备都可以看做一个文件来操作.那么我们对与外部设备的操作都可以看做对文件进行操作.我们对一个文件的读写,都通过调用内核提供的系统调用:内核给我们返回一个file ...
- Taxi
/* After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to cele ...
- 如何搭建http服务仓库
1.拷贝仓库repo-A文件到服务器/media/D: 2.通过createrepo_c 生成仓库rpm信息数据 cd repo-A createrepo . 3.chmod -R 775 repo ...
- 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...
- 人类及其他物种基因组DNA之问
问题1 : 不同人类个体的基因组长度总长是不是一样,如果不一样,那么人类基因组长度排序和范围区间是如何控制的?最短是多少,最长是多少?如果一样,如何理解基因的插入与缺失,INDEL等现象,如何平衡的呢 ...
- WCF 与 Windows Store Client App
首先复习下WCF: WCF实际上是构建了一个框架,这个框架实现了在互联系统中各个Application之间如何通信.使得Developers和Architect在构建分布式系统中,无需在考虑如何去实现 ...
- 使用Simple MvvmToolkit开发Android和iOS程序
详情见:Android and iOS Development with Simple MVVM Toolkit? Yes you can! :http://blog.tonysneed.com/20 ...
- TCP与UDP传输协议
目录结构: contents structure [-] 1 TCP协议和UDP协议的比较 1.1 TCP协议 TCP的全称是Transmission Control Protocol (传输控制协议 ...