日期:2014-05-18  浏览次数:20525 次

树形结构表查询,只用一条SQL语句查询所有父级ID
表字段:ID,Tname,Pid  
Pid是上级ID

现在通过ID查询出所有父级ID。

只用一条SQL语句。不能用存储过程、函数

面试时他要求只要说出使用到的关键字就行了。

------解决方案--------------------
SQL code
;with cte as
(
select * from tb where id=@id
union all
select a.* from tb a join cte b on a.id=b.pid
)

select * from cte