Oracle CTE Merge

我与影子孤独终老i 提交于 2019-12-10 16:33:24

问题


I am trying a simple merge statement using a CTE(Common table expression) . But it gives an error

MERGE INTO  emp targ USING (
*
ERROR at line 4:
ORA-00928: missing SELECT keyword

Is the CTE not allowed in a merge statement? My Sql is below:

WITH cte AS (
  SELECT empno, ename 
    FROM EMP)
MERGE INTO emp targ USING (SELECT * 
                             FROM cte) src
  ON (targ.empno = src.empno)
WHEN MATCHED THEN update 
   SET targ.ename = src.ename
WHEN NOT MATCHED THEN insert
    (empno,ename)
  VALUES
    (src.empno,src.ename)
/

回答1:


The WITH clause is for use with a SELECT statement.

From the documents: "You can specify this clause in any top-level SELECT statement and in most types of subqueries." (emphasis mine).

Here is a possible workaround if you really need to do this, from ORAFAQ. The main blog writeup is here.



来源:https://stackoverflow.com/questions/6064970/oracle-cte-merge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!