1:遞歸查詢子信息,並查詢出多少條子信息
遞歸
WITH CTE AS (SELECT ORG_ID, PARENT_ORG_ID, ORG_NAME, SORT FROM SYS_ORG WHERE 1=1 AND ORG_ID='F80FE6B0-4A83-46F5-B78C-A17CC49AD172' UNION ALL SELECT C.ORG_ID, C.PARENT_ORG_ID, C.ORG_NAME, C.SORT FROM SYS_ORG C INNER JOIN CTE P ON P.ORG_ID = C.PARENT_ORG_ID) SELECT ORG_ID, PARENT_ORG_ID, ORG_NAME, SORT, ( SELECT CAST(COUNT(*) AS DECIMAL) FROM CTE C1 WHERE C1.PARENT_ORG_ID = C2.ORG_ID ) CHILD_COUNT FROM CTE C2 ORDER BY PARENT_ORG_ID, SORT
2:遞歸查詢子信息
WITH CTE AS (SELECT ORG_ID, PARENT_ORG_ID, ORG_NAME, SORT FROM SYS_ORG WHERE 1=1 AND ORG_ID='F80FE6B0-4A83-46F5-B78C-A17CC49AD172' UNION ALL SELECT C.ORG_ID, C.PARENT_ORG_ID, C.ORG_NAME, C.SORT FROM SYS_ORG C INNER JOIN CTE P ON P.ORG_ID = C.PARENT_ORG_ID) SELECT ORG_ID, PARENT_ORG_ID, ORG_NAME, SORT FROM CTE ORDER BY PARENT_ORG_ID, SORT