我有父子關係SQL表遞歸CTE SQL - 查找下一個可用的家長
LOCATIONDETAIL Table
OID NAME PARENTOID
1 HeadSite 0
2 Subsite1 1
3 subsite2 1
4 subsubsite1 2
5 subsubsite2 2
6 subsubsite3 3
RULESETCONFIG
OID LOCATIONDETAILOID VALUE
1 1 30
2 4 15
如果我提供的輸入作爲LOCATIONDETAIL 6,我應該得到RULESETCONFIG值30 因爲 LOCATIONDETAIL 6,parentId的是3和用於LOCATIONDETAIL 3存在RULESETCONFIG沒有值, LOCATIONDETAIL 3具有其在RULESETCONFIG
值,如果我提供輸入作爲LOCATIONDETAIL 4父1,我應該得到RULESETCONFIG值15
我有代碼來填充樹,但不知道如何找到下一個可用的家長
;WITH GLOBALHIERARCHY AS
(
SELECT A.OID,A.PARENTOID,A.NAME
FROM LOCATIONDETAIL A
WHERE OID = @LOCATIONDETAILOID
UNION ALL
SELECT A.OID,A.PARENTOID,A.NAME
FROM LOCATIONDETAIL A INNER JOIN GLOBALHIERARCHY GH ON A.PARENTOID = GH.OID
)
SELECT * FROM GLOBALHIERARCHY