查詢某節點的所有父節點(包含自己)
with
RTU1 as(
select TDtID ,TDtPID,[TDtName] from TDept
),
RTU2 as(
select * from RTU1 where TDtID=1
union all
select RTU1.*from RTU2 inner join RTU1
--on myT2.id=myT.PID
on RTU2.TDtPID=RTU1.TDtID
)
select * from RTU2;
查詢某節點的所有子節點(包含自己)
with
RTD1 as(
select TDtID ,TDtPID,[TDtName] from TDept
),
RTD2 as( select * from RTD1 where TDtID=7
union all
select RTD1.* from RTD2 inner join RTD1
on RTD2.TDtID=RTD1.TDtpID )
select * from RTD2