1. 程式人生 > >sqlserver查詢指定樹形結構的所有子節點

sqlserver查詢指定樹形結構的所有子節點

best vps 指定 serve baidu sele text sub aid

用標準sql的with實現遞歸查詢

with subqry(id,name,pid) as (
  select id,name,pid from test1 where id = 5 --指定id
  union all
  select test1.id,test1.name,test1.pid from test1,subqry where test1.pid = subqry.id
)
select* from subqry;

sqlserver查詢指定樹形結構的所有子節點