SQL 用;with 由所有的子节点查询到树结构中所有父节点

1、所有的子节点查询到树结构中所有父节点

RETURNS @Tree Table(PID int,FID int ,Name VARCHAR(300))
as
begin 
--DECLARE @ID VARCHAR(3)
--SET @ID = '16'
;with rolDepList as
(
  select PID,FID,Name from tbDepList as b where exists(select PID from tbUserDep where DepID=b.PID and UserID = @ID) AND (CanUse = 1)
  union all
  select a.PID,a.FID,a.Name from tbDepList  a join rolDepList b on a.pid=b.fid
)
 Insert @Tree select distinct(PID),FID,Name from rolDepList
--'select distinct(PID),FID,Name from rolDepList 

Return

end 

函数调用:

select PID,FID,Name from dbo.GetRolDep(16)

2、获取子节点的所有父节点集合

DECLARE @ID VARCHAR(8)
SET @ID = '16'
;with getDepList as
(
  select * from tbDepList where pid=@id
  union all
  select a.* from tbDepList a join getDepList b on a.pid=b.fid
)
 
select * from getDepList 

 

转载于:https://www.cnblogs.com/yimeishui/p/5844455.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值