Oracle中缺少右括号错误

时间:2015-02-16 09:12:53

标签: sql sql-server oracle tsql plsql

以下查询在SQL Server 2012中正常运行。但是当我尝试在Oracle上运行它时,它会给出 ORA-00907:错过右括号错误。无法弄清楚我在这里做错了什么。 / p>

SELECT p1.ACTION_ID,
       STUFF( (SELECT ',  '+EMP_DISPLAY_NAME
               FROM ( SELECT DISTINCT EMP.EMP_DISPLAY_NAME,ACTS.ACTION_ID
                      FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
                      LEFT JOIN HS_HR_EMPLOYEE EMP
                          ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
                      LEFT JOIN HS_HR_OB_ACTION ACTS
                          ON ACTS.ACTION_ID=ALT.ACTION_ID) p2
               WHERE p2.ACTION_ID = p1.ACTION_ID
               ORDER BY EMP_DISPLAY_NAME
               FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
            ,1,3,'')
       AS NAME
FROM ( SELECT DISTINCT EMP.EMP_DISPLAY_NAME,ACTS.ACTION_ID
       FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
       LEFT JOIN HS_HR_EMPLOYEE EMP
           ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
       LEFT JOIN HS_HR_OB_ACTION ACTS
           ON ACTS.ACTION_ID=ALT.ACTION_ID) p1
GROUP BY ACTION_ID 

3 个答案:

答案 0 :(得分:1)

  

SELECT',' + EMP_DISPLAY_NAME

它不是有效的Oracle语法。您需要使用连接运算符||

例如,SELECT EMP_DISPLAY_NAME||' ,'

测试用例 -

SQL> SELECT 'Employee ID is '||empno FROM emp;

'EMPLOYEEIDIS'||EMPNO
----------------------------------------------
Employee ID is 7369
Employee ID is 7499
Employee ID is 7521
Employee ID is 7566
Employee ID is 7654
Employee ID is 7698
Employee ID is 7782
Employee ID is 7788
Employee ID is 7839
Employee ID is 7844
Employee ID is 7876
Employee ID is 7900
Employee ID is 7902
Employee ID is 7934

14 rows selected.

SQL>

答案 1 :(得分:1)

从您的查询中我得出最小的数据结构:

HS_HR_OB_ACTION (action_id number)
HS_HR_EMPLOYEE (EMP_NUMBER number, emp_display_name varchar2(20))
HS_HR_OB_ACT_ALT_RECIPIENT (ACTALT_EMP_NUMBER number, action_id number)

对于此类数据,以下查询在Oracle 10g中有效。对于Oracle 11取消注释第二行和注释第三行。请将结果与SQL Server进行比较。

SELECT p1.ACTION_ID,
    -- (select LISTAGG(emp_display_name, ', ') WITHIN GROUP (ORDER BY emp_display_name)
    (SELECT wmsys.wm_concat(EMP_DISPLAY_NAME)
       FROM (
         SELECT DISTINCT EMP.EMP_DISPLAY_NAME,ACTS.ACTION_ID
           FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
             LEFT JOIN HS_HR_EMPLOYEE EMP ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
             LEFT JOIN HS_HR_OB_ACTION ACTS ON ACTS.ACTION_ID=ALT.ACTION_ID
         ) p2
       WHERE p2.ACTION_ID = p1.ACTION_ID) AS NAME
  FROM ( 
    SELECT DISTINCT EMP.EMP_DISPLAY_NAME,ACTS.ACTION_ID
      FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
        LEFT JOIN HS_HR_EMPLOYEE EMP ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
        LEFT JOIN HS_HR_OB_ACTION ACTS ON ACTS.ACTION_ID=ALT.ACTION_ID
    ) p1
  GROUP BY ACTION_ID

我认为此查询可能已经简化,但我最初并不想过多地干涉原始版本。

SELECT distinct ACTION_ID, wmsys.wm_concat(EMP_DISPLAY_NAME) -- or listagg(...)
  FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
    LEFT JOIN HS_HR_EMPLOYEE EMP ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
    LEFT JOIN HS_HR_OB_ACTION ACTS ON ACTS.ACTION_ID=ALT.ACTION_ID
  GROUP BY ACTION_ID

答案 2 :(得分:0)

我为Ponder Stibbons做的一些修改回答了与我合作,因为他们给出了 CLOB 错误。谢谢@Ponder Stibbons。

SELECT p1.ACTION_ID,
    -- (select LISTAGG(emp_display_name, ',') WITHIN GROUP (ORDER BY emp_display_name)
    (SELECT dbms_lob.substr( wmsys.wm_concat(EMP_DISPLAY_NAME), 4000, 1 ) 
       FROM (
         SELECT DISTINCT EMP.EMP_DISPLAY_NAME,ACTS.ACTION_ID
           FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
             LEFT JOIN HS_HR_EMPLOYEE EMP ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
             LEFT JOIN HS_HR_OB_ACTION ACTS ON ACTS.ACTION_ID=ALT.ACTION_ID
         ) p2
       WHERE p2.ACTION_ID = p1.ACTION_ID) AS NAME
  FROM ( 
    SELECT DISTINCT EMP.EMP_DISPLAY_NAME,ACTS.ACTION_ID
      FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
        LEFT JOIN HS_HR_EMPLOYEE EMP ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
        LEFT JOIN HS_HR_OB_ACTION ACTS ON ACTS.ACTION_ID=ALT.ACTION_ID
    ) p1
  GROUP BY ACTION_ID

这也很好。

  SELECT distinct ACTS.ACTION_ID, dbms_lob.substr( wmsys.wm_concat(EMP_DISPLAY_NAME), 4000, 1 )
  FROM HS_HR_OB_ACT_ALT_RECIPIENT ALT
    LEFT JOIN HS_HR_EMPLOYEE EMP ON EMP.EMP_NUMBER=ALT.ACTALT_EMP_NUMBER
    LEFT JOIN HS_HR_OB_ACTION ACTS ON ACTS.ACTION_ID=ALT.ACTION_ID
  GROUP BY ACTS.ACTION_ID