with a as ( select id,name,parentid from categories where id=53 union all select x.id,x.name,x.parentid from categories x,a where x.parentid=a.id ) select * from a
父找子 with RTD1 as( select Id,pid from Sys_XCode ), RTD2 as( select * from RTD1 where id=1 union all select RTD1.* from RTD2 inner join RTD1 on RTD2.id=RTD1.PID ) select * from RTD2 子找父 with RTU1 as( select id ,pid from UserGroup
SQLserver2008使用表达式递归查询语句 --由父项递归下级 with cte(id,parentid,text) as (--父项 select id,parentid,text from treeview where parentid = 450 union all --递归结果集中的下级 select t.id,t.parentid,t.text from treeview as t inner join cte as c on t.parentid = c.id ) select