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 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM