1.根据节点主键,查询当前节点和所有子节点
WITH SOURCE AS ( SELECT * FROM TableName WHERE DeleteState = 0 ), LIST AS ( SELECT value FROM STRING_SPLIT('2111011043240001', ',') ), RESULT AS ( SELECT SOURCE.* FROM SOURCE INNER JOIN LIST ON SOURCE.Code = LIST.value UNION ALL SELECT SOURCE.* FROM RESULT INNER JOIN SOURCE ON RESULT.Code = SOURCE.ParentCode ) SELECT Code, ParentCode, NodeName, NodeIndex, IsLastNode FROM RESULT
2.根据节点主键,查询当前节点和所有父节点
WITH SOURCE AS ( SELECT * FROM TableName WHERE DeleteState = 0 ), LIST AS ( SELECT value FROM STRING_SPLIT('2111011043260028', ',') ), RESULT AS ( SELECT SOURCE.* FROM SOURCE INNER JOIN LIST ON SOURCE.Code = LIST.value UNION ALL SELECT SOURCE.* FROM RESULT INNER JOIN SOURCE ON RESULT.ParentCode = SOURCE.Code ) SELECT Code, ParentCode, NodeName, NodeIndex, IsLastNode FROM RESULT