博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在SQL中递归查询数据
阅读量:4128 次
发布时间:2019-05-25

本文共 1789 字,大约阅读时间需要 5 分钟。

需求如下:

 

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://zqzvi.baihongyu.com/

你可能感兴趣的文章
20种在学习编程的同时也可以在线赚钱的方法
查看>>
隐藏搜索框:CSS 动画正反向序列
查看>>
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
8种ES6中扩展运算符的用法
查看>>
【视频教程】Javascript ES6 教程28—ES6 Promise 实例应用
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>