我想检索出一个id的parentid,如果该parentid再次有父元素,请检索它,以此类推。这是一种层次结构表。
id----parentid
1-----1
5-----1
47894--5
47897--47894
我是 SQL Server 的新手,并尝试了一些查询,如:
with name_tree as
(
select id, parentid
from Users
where id = 47897 -- this is the starting point you want in your recursion
union all
select c.id, c.parentid
from users c
join name_tree p on p.id = c.parentid -- this is the recursion
)
select *
from name_tree;
它只给了我一行记录。我还想将这些记录插入到一个临时表变量中。我该怎么做。谢谢你提前。对于问这个简单问题(虽然不是对我来说)感到抱歉。