在SQL Server中使用RECURSIVE CTE时,不应使用GROUP BY
,LEFT OUTER JOIN
和ORDER BY
等。
如何将此ORACLE查询转换为SQL Server?
SELECT *
FROM (SELECT COALESCE(a.tree_id, ' ') MNUID,
COALESCE(a.tree_nm, ' ') MNUNM,
COALESCE(A.TREE_LEV_CD, ' ') MNULEVCD,
COALESCE(A.UP_TREE_ID, ' ') UPMNUID,
COALESCE(A.TREE_CD, ' ') TREECD,
COALESCE(A.TREE_LRK_RUF_ID, ' ') SCEXRSSEQVL,
CASE WHEN B.SCH_CNT=NULL THEN 0 ELSE B.SCH_CNT END AS SCHCNT
FROM SC_TREE_MASTER A
LEFT OUTER JOIN
(SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
FROM SC_TREE_MASTER C
LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
WHERE C.TREE_BJ_CD = '01'
AND C.TREE_CD = '04'
GROUP BY TREE_LRK_RUF_ID) B
ON (A.TREE_BJ_CD = '01' AND A.TREE_LRK_RUF_ID = B.TREE_LRK_RUF_ID)
ORDER BY MNULEVCD, SCEXRSSEQVL)
START WITH UPMNUID = 'TR00000000'
CONNECT BY PRIOR MNUID = UPMNUID
ORDER SIBLINGS BY MNUID;
到目前为止我试过了。
但是发生了错误。
WITH TEMP_TAB(MNUID, MNUNM, MNULEVCD, UPMNUID, TREECD, TREE_LRK_RUF_ID, SORT, RECURSIVE_LEVEL) AS
(SELECT TREE_ID,
TREE_NM,
TREE_LEV_CD,
UP_TREE_ID,
TREE_CD,
TREE_LRK_RUF_ID,
CONCAT('-',TREE_ID) AS SORT,
1 AS RECURSIVE_LEVEL
FROM SC_TREE_MASTER
WHERE TREE_BJ_CD = '01'
AND UP_TREE_ID = 'TR00000000'
UNION ALL
SELECT A.TREE_ID,
A.TREE_NM,
A.TREE_LEV_CD,
A.UP_TREE_ID,
A.TREE_CD,
A.TREE_LRK_RUF_ID,
CONCAT(B.SORT,'/',A.TREE_ID) AS SORT,
(RECURSIVE_LEVEL + 1) AS RECURSIVE_LEVEL
FROM SC_TREE_MASTER A, TEMP_TAB B
WHERE A.TREE_BJ_CD = '01'
AND B.MNUID =A.UP_TREE_ID
)
SELECT COALESCE(M.TREE_ID, ' ') MNUID,
COALESCE(M.TREE_NM, ' ') MNUNM,
COALESCE(M.TREE_LEV_CD, ' ') MNULEVCD,
COALESCE(M.UP_TREE_ID, ' ') UPMNUID,
COALESCE(M.TREE_CD, ' ') TREECD,
COALESCE(M.TREE_LRK_RUF_ID, ' ') TREE_LRK_RUF_ID
FROM TEMP_TAB M
LEFT OUTER JOIN (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
FROM SC_TREE_MASTER C
LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
WHERE C.TREE_BJ_CD = '01'
AND C.TREE_CD = '04'
GROUP BY TREE_LRK_RUF_ID) N
ON M.TREE_LRK_RUF_ID = N.TREE_LRK_RUF_ID
ORDER BY SORT;
类型不在锚和列中的递归部分之间匹配 " SORT"递归查询" TEMP_TAB"。
另一个问题:如何将ORACLE中的connect by
转换为ANSI以用于所有数据库系统?
不可能吗?
我添加了这个查询,可以在下面的POSTGRESQL中使用。
我认为此查询与SQL Server RECURSIVE-CTE查询类似,但在SQL Server中,它不起作用。
WITH RECURSIVE TEMP_TAB(TREE_ID,
TREE_NM,
TREE_LEV_CD,
UP_TREE_ID,
TREE_CD,
TREE_LRK_RUF_ID,
SORT ,
RECURSIVE_LEVEL) AS
(SELECT TREE_ID,
TREE_NM,
TREE_LEV_CD,
UP_TREE_ID,
TREE_CD,
TREE_LRK_RUF_ID,
CONCAT('-',TREE_ID) ,
1 RECURSIVE_LEVEL
FROM SC_TREE_MASTER
WHERE TREE_BJ_CD = '01'
AND UP_TREE_ID = 'TR00000000'
UNION ALL
SELECT A.TREE_ID,
A.TREE_NM,
A.TREE_LEV_CD,
A.UP_TREE_ID,
A.TREE_CD,
A.TREE_LRK_RUF_ID,
CONCAT(B.SORT,'/',A.TREE_ID),
RECURSIVE_LEVEL + 1
FROM SC_TREE_MASTER A, TEMP_TAB B
WHERE A.TREE_BJ_CD = '01'
AND B.TREE_ID =A.UP_TREE_ID
)
SELECT COALESCE(M.TREE_ID, ' ') MNUID,
COALESCE(M.TREE_NM, ' ') MNUNM,
COALESCE(M.TREE_LEV_CD, ' ') MNULEVCD,
COALESCE(M.UP_TREE_ID, ' ') UPMNUID,
COALESCE(M.TREE_CD, ' ') TREECD,
COALESCE(M.TREE_LRK_RUF_ID, ' ') SCEXRSSEQVL,
COALESCE(N.SCH_CNT, 0)
FROM TEMP_TAB M
LEFT OUTER JOIN (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
FROM SC_TREE_MASTER C
LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
WHERE C.TREE_BJ_CD = '01'
AND C.TREE_CD = '04'
GROUP BY TREE_LRK_RUF_ID) N
ON M.TREE_LRK_RUF_ID = N.TREE_LRK_RUF_ID
ORDER BY SORT;
答案 0 :(得分:0)
WITH TEMP_TAB(TREE_ID,
TREE_NM,
TREE_LEV_CD,
UP_TREE_ID,
TREE_CD,
TREE_LRK_RUF_ID,
SORT,
RECURSIVE_LEVEL) AS
(SELECT TREE_ID,
TREE_NM,
TREE_LEV_CD,
UP_TREE_ID,
TREE_CD,
TREE_LRK_RUF_ID,
CAST(CONCAT('-',TREE_ID) AS VARCHAR(100)),
1 RECURSIVE_LEVEL
FROM SC_TREE_MASTER
WHERE TREE_BJ_CD = '01'
AND UP_TREE_ID = 'TR00000000'
UNION ALL
SELECT A.TREE_ID,
A.TREE_NM,
A.TREE_LEV_CD,
A.UP_TREE_ID,
A.TREE_CD,
A.TREE_LRK_RUF_ID,
CAST(CONCAT(B.SORT,'/',A.TREE_ID) AS VARCHAR(100)),
RECURSIVE_LEVEL + 1
FROM SC_TREE_MASTER A, TEMP_TAB B
WHERE A.TREE_BJ_CD = '01'
AND B.TREE_ID =A.UP_TREE_ID
)
SELECT COALESCE(M.TREE_ID, ' ') MNUID,
COALESCE(M.TREE_NM, ' ') MNUNM,
COALESCE(M.TREE_LEV_CD, ' ') MNULEVCD,
COALESCE(M.UP_TREE_ID, ' ') UPMNUID,
COALESCE(M.TREE_CD, ' ') TREECD,
COALESCE(M.TREE_LRK_RUF_ID, ' ') SCEXRSSEQVL,
COALESCE(N.SCH_CNT, 0)
FROM TEMP_TAB M
LEFT OUTER JOIN (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
FROM SC_TREE_MASTER C
LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
WHERE C.TREE_BJ_CD = '01'
AND C.TREE_CD = '04'
GROUP BY TREE_LRK_RUF_ID) N
ON M.TREE_LRK_RUF_ID = N.TREE_LRK_RUF_ID
ORDER BY SORT;