公用表表达式简介:

公用表表达式 (CTE) 可以认为是在单个 SELECT、INSERT、UPDATE、DELETE 或 CREATE VIEW 语句的执行范围内定义的临时结果集。CTE 与派生表类似,具体表现在不存储为对象,并且只在查询期间有效。与派生表的不同之处在于,公用表表达式 (CTE) 具有一个重要的优点,那就是能够引用其自身,从而创建递归 CTE。递归 CTE 是一个重复执行初始 CTE 以返回数据子集直到获取完整结果集的公用表表达式。

下面先创建一个表,并插入一些数据:

create table Role_CTE( Id  int    not null, Name nvarchar(32) not null, ParentId int  not null )insert into Role_CTE(Id,Name,ParentId)select '1','超级管理员','0' union select '2','管理员A','1' union select '3','管理员B','2' union select '4','会员AA','2' union select '5','会员AB','2' union select '6','会员BA','3' union select '7','会员BB','3' union select '8','用户AAA','4' union select '9','用户BBA','7' -- 创建一个复合聚集索引create clustered index Clu_Role_CTE_Indexon Role_CTE(Id,ParentId)with( pad_index=on, fillfactor=50, drop_existing=off, statistics_norecompute=on)select * from Role_CTE

使用普通 sql 语句实现:

declare @level intdeclare @node intdeclare @ResTab table( node int not null, lv  int not null )set @level=0  -- 表示初始的等级set @node=3   --表示初始的节点ID,即从指定的哪个节点开始查找insert into @ResTab    -- 为表变量插入初始的数据select Id,@level from Role_CTE where Id=@nodewhile(@@ROWCOUNT>0)begin set @level=@level+1 insert into @ResTab select b.Id,@level  from @ResTab a  join Role_CTE b on a.node=b.ParentId and lv=@level-1 -- join 等于 inner join(内连接)和自连接endselect a.node,b.Name,a.lv from @ResTab a left join Role_CTE b on a.node=b.Id

PS:lv=@level-1 是重点,不然会进入死循环,作用就是限制只插入一次。

如果需要限制循环的次数,即递归的层数,那么只需要在 while 条件里面添加一个限制即可。如下:

declare @level intdeclare @node intdeclare @num intdeclare @ResTab table( node int not null, lv  int not null )set @level=0  -- 表示初始的等级set @node=3   --表示初始的节点ID,即从指定的哪个节点开始查找set @num=1  -- 指定递归层级,即循环的次数insert into @ResTab    -- 为表变量插入初始的数据select Id,@level from Role_CTE where Id=@nodewhile(@@ROWCOUNT>0 and @level<@num)begin set @level=@level+1 insert into @ResTab select b.Id,@level  from @ResTab a  join Role_CTE b on a.node=b.ParentId and lv=@level-1 -- join 等于 inner join(内连接)和自连接endselect a.node,b.Name,a.lv from @ResTab a left join Role_CTE b on a.node=b.Id

使用 SQL CTE 实现:

declare @node int set @node=3;with temp_cteas( select Id,Name,0 lv  -- 查询出“根节点”,即指定的起始节点 from Role_CTE  where Id=@node  union all select b.Id,b.Name,a.lv+1  from temp_cte a  join Role_CTE b on a.Id=b.ParentId)select * from temp_cte
declare @node int declare @num intset @node=3;set @num=1;with temp_cteas( select Id,Name,0 lv  -- 查询出“根节点”,即指定的起始节点 from Role_CTE  where Id=@node  union all select b.Id,b.Name,a.lv+1  from temp_cte a  join Role_CTE b on a.Id=b.ParentId     and a.lv<@num  --控制递归层数)select * from temp_cte

使用普通 sql 语句实现:

declare @level intdeclare @node intdeclare @num intdeclare @ResTab table( node int not null, lv  int not null )set @level=0 -- 表示初始的等级set @node=8   --表示初始的节点ID,即从指定的哪个节点开始查找set @num=2  -- 指定递归层级,即循环的次数while(@level<=@num and @node is not null) -- 如果为空就表示没有查到父级了begin insert into @ResTab select @node,@level set @level=@level+1 select @node=ParentId  from Role_CTE  where Id=@nodeendselect a.node,b.Name,a.lv from @ResTab a left join Role_CTE b on a.node=b.Id
declare @node int declare @num intset @node=8;set @num=2;with temp_cteas( select Id,Name,ParentId,0 lv  -- 查询出“根节点”,即指定的起始节点 from Role_CTE  where Id=@node  union all select b.Id,b.Name,b.ParentId,a.lv+1  from temp_cte a  join Role_CTE b on a.ParentId=b.Id     and a.lv < @num  --控制递归层数)select * from temp_cte

更多相关文章

  1. MySQL 5.7.9 服务无法启动-“NET HELPMSG 3534”的解决方法
  2. ES6 变量声明,箭头函数,数组方法,解构赋值,JSON,类与继承,模块化练习
  3. 浅谈Java中Collections.sort对List排序的两种方法
  4. Python list sort方法的具体使用
  5. python list.sort()根据多个关键字排序的方法实现
  6. android上一些方法的区别和用法的注意事项
  7. android实现字体闪烁动画的方法
  8. Android中dispatchDraw分析
  9. Android四大基本组件介绍与生命周期

随机推荐

  1. android 默认浏览器 无法下载,此手机不支
  2. 【30篇突击 android】源码统计 十三
  3. android ATD configeration
  4. android之屏幕适配之一理论知识
  5. android传感器总结
  6. 【Demo实例】Android FTP上传带进度条(优
  7. Android通过http协议POST传输方式(输出流
  8. 使用InjectView和findViewById说拜拜
  9. Android SDK 源码解析项目
  10. Android 获取当前日期 时间