s查找父节点

时间:2022-05-16 20:04:16

查找所有的父节点,包括本身,不包括就<>id

with tbs as(
select * from TB_HomeBase where ID=223 union all
select a.* from TB_HomeBase a inner join tbs b on a.id=b.ParentID) select top 1 ID FROM ( select *,ROW_NUMBER() over (order by FirstID)as tt from tbs) AS A ORDER BY tt DESC

ROW_NUMBER() over (order by FirstID)as tt:给他增加一个排序id