需求如下:
ID 是否为部门 部门名 上级ID
1 y 部门0 1
31 y 部门1 1
32 n 张三 31
33 n 李二 31
34 y 部门2 31
35 n 王五 34
35 y 部门3 34
36 n 小三 35
查询 ID 值为 35 下级的所有人员包括下级部门的所有人员
--创建查询函数
create function f_id(
@id int --要查询的id
)returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select id,@l
from 表
where 上级id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from 表 a join @re b on a.上级id=b.id and b.level=@l-1
end
return
end
go
--调用函数进行查询
select a.* from 表 a join f_id(35) b on a.id=b.id
--测试
--测试数据
create table 表(ID int,是否为部门 char(1),部门名 varchar(10),上级ID int)
insert 表 select 1 ,'y','部门0' ,1
union all select 31,'y','部门1' ,1
union all select 32,'n','张三' ,31
union all select 33,'n','李二' ,31
union all select 34,'y','部门2',31
union all select 35,'n','王五' ,34
union all select 35,'y','部门3',34
union all select 36,'n','小三' ,35
go
--创建查询函数
create function f_id(
@id int --要查询的id
)returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select id,@l
from 表
where 上级id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from 表 a join @re b on a.上级id=b.id and b.level=@l-1
end
return
end
go
--调用函数进行查询
select a.* from 表 a join f_id(35) b on a.id=b.id
go
--删除测试
drop table 表
drop function f_id
/*--测试结果
ID 是否为部门 部门名 上级ID
----------- ----- ---------- -----------
36 n 小三 35
(所影响的行数为 1 行)
--*/
--对函数及调用的一些说明.
--创建查询函数
create function f_id(
@id int --要查询的id
)returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select id,@l
from 表
where 上级id=@id --如果要包含查询的id,改条件为:id=@id
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.id,@l
from 表 a join @re b on a.上级id=b.id and b.level=@l-1
end
return
end
go
--调用函数进行查询
select a.* from 表 a join f_id(35) b on a.id=b.id
--where a.是否为部门='n' --如果只显示人员,加上条件
下面两篇文章给出的方法也值得参考一下:
http://blog.chinaunix.net/u/25096/showart_369590.html
http://www.cnblogs.com/Luoke365/archive/2008/03/24/1120241.html
分享到:
相关推荐
sql server 2008 递归查询所有上级或下级数据
给定一个节点ID,查询此节点下的全部节点,并将结果集保存在临时表中
sqlserver递归查询树结构的表的根结点子最里层的子结点
想用递归sql语句把所有属于某个upperid的数据,包括它的子树,都读出去, 比如说 upperid =2 那么先找到1,3,然后再由1,3找到4,5 id upperid ----------- ----------- 1 2 3 2 4 1 5 3 6 4 upperid=2查询结果 id ...
部分普通sql查询在hive中的实现方式详细说明;
表的所有列名 select name from sysobjects where xtype=’u’ select name from syscolumns where id=(select max(id) from sysobjects where xtype=’u’ and name=’表名’)☆ 递归查询数据Sql语句里的递归查询...
将多行数据生成一个一个字符患的递归脚本,方便,快捷。要顶哦。
浅谈T-SQL语言之递归查询.pdf
sql server 通过CTE来支持递归查询,这对查询树形或层次结构的数据很有用 一般的树形表结构如下,相信大家都很熟悉的 id title pid 1 1级节点 0 2 2级节点 1 3 3级节点 2 4 4级节点 3 5 5级节点 4 ...
例如,现在需要查询出西湖区以及西湖区所属的市和省,这时候就需要用到SQL递归查询。我在这里构造了一张数据表[tb_Test],其数据如下所示: Id Name ParentId 1 浙江省 NULL 2 杭州市 1 3 湖州市 1 4 滨江区 2 ...
SQL sever 中递归查找子节点和父节点,有实例SQL代码,直接运行
全国省市区镇数据(excel表格,少部分递归,完整sql数据库备份)多种数据源
一种MySQL数据库SQL递归查询的研究与实现.pdf
本文给大家介绍有关数据库SQL递归查询在不同数据库中的实现方法,具体内容请看下文。 比如表结构数据如下: Table:Tree ID Name ParentId 1 一级 0 2 二级 1 3 三级 2 4 四级 3 SQL SERVER 2005查询方法: ...
当初花了很长时间查资料、逛博客 终于搞定的排序 以及对于处理菜单列表显示的 问题。希望对你们有帮助。压缩包包含数据表创建脚本 以及 实现功能的语句 表结构字段 id、name、url、pid、indexs
T-SQL中的递归.pdf
在查询中用TOP选项修改数据;用递归逻辑、具体化路径或嵌套集合解决方案查询特殊的数据结构;通过逻辑难题提高你的逻辑能力并掌握查询问题的核心等。 你将学习到如何: 理解逻辑和物理的查询处理; 使用方法论...
主要内容包括SQL的基础理论、查询优化、查询算法及复杂度,以及在使用子查询、表表达式、排名函数、数据聚合和透视转换、TOP和APPLY、数据修改、分区表、特殊数据结构等实际应用时会遇到的各种高级查询问题和解决...
SQL Server 存储层级数据实现无限级分类,非递归存储过程无限级分类,左右值分类