树形结构表查询,只用一条SQL语句查询所有父级ID表字段:ID,Tname,Pid Pid是上级ID现在通过ID查询出所有父级ID。只用一条SQL语句。不能用存储过程、函数面试时他要求只要说出使用到的关键字就行了。[解决办法]
;with cte as(select * from tb where id=@idunion allselect a.* from tb a join cte b on a.id=b.pid)select * from cte