我正在尝试获取孩子所拥有的所有父母的名单,直到到达上级父母为止。因此,如果我给该语句一个Rubrieknr,则需要返回所有上级父母,最后是头级父母
我尝试使用UNION函数,但似乎无法解决问题。
WITH n(Rubrieknr, Rubrieknaam) AS
(SELECT Rubrieknr, Rubrieknaam
FROM Rubriek
WHERE Rubrieknr = (SELECT TOP 1 Rubrieknr FROM Rubriek WHERE RubriekParent IS NULL)
UNION ALL
SELECT nplus1.Rubrieknr, nplus1.Rubrieknaam
FROM Rubriek as nplus1, n
WHERE n.Rubrieknr = nplus1.RubriekParent)
SELECT Rubrieknr, Rubrieknaam FROM n
我希望它返回如下内容:
parent
child
child
child
*selected child*