sql server使用cte实现树结构递归查询
2016-07-08 16:10
387 查看
sql server使用cte实现树结构递归查询
use
xxx
go
Begin
--获得所有有权限的叶子节点
Declare
@Tempsubqrys
TABLE(
id
int,
Parentid
int,
planname
nvarchar(50),
Grd
int
);
with
subqry(id,Parentid,planname,Grd)
as (
select
id,Parentid,planname,Grd
from
T_Sch_Plan
where
id
= 2
union
all
select
t.id,t.Parentid,t.planname,t.Grd
from
T_Sch_Plan
t,subqry
where
t.parentid
=
subqry.id
)
insert
@Tempsubqrys(id,Parentid,planname,Grd)
(select
*
from
subqry
where
subqry.Grd
=4 and
subqry.id
not
in
(select
Planid
from
T_Sch_Plan_User
where
Fesid=1));
select
*
from
@Tempsubqrys;
end
use
xxx
go
Begin
--获得所有有权限的叶子节点
Declare
@Tempsubqrys
TABLE(
id
int,
Parentid
int,
planname
nvarchar(50),
Grd
int
);
with
subqry(id,Parentid,planname,Grd)
as (
select
id,Parentid,planname,Grd
from
T_Sch_Plan
where
id
= 2
union
all
select
t.id,t.Parentid,t.planname,t.Grd
from
T_Sch_Plan
t,subqry
where
t.parentid
=
subqry.id
)
insert
@Tempsubqrys(id,Parentid,planname,Grd)
(select
*
from
subqry
where
subqry.Grd
=4 and
subqry.id
not
in
(select
Planid
from
T_Sch_Plan_User
where
Fesid=1));
select
*
from
@Tempsubqrys;
end
相关文章推荐
- mysql中if的另外用法
- Oracle写没有表可以创建表
- redis事务管理
- MySQL的安装和使用
- SQL Server还原数据库后,数据库显示【正在还原】的解决方法
- SQL中的case when then else end用法
- MySQL学习笔记汇总
- Oracle查询哪个表锁住
- Oracle-The Java Tutorials
- My SQL
- Oracle连接两个数据库
- 数据库连接慢,添加反向DNS反向解析的主机
- Redis的HA搭建和主从复制搭建以及测试
- 彻底删除Oracle的手顺
- 3、MySQL基本操作-----第2篇
- MongoDB从环境搭建到代码编程(Window 环境)
- 优化临时表使用,SQL语句性能提升100倍
- oracle主键外键以及级联操作
- SQl查询数据库表名、表的列名、数据类型
- 连接数据库的SID错误的解决方法