在Oracle上使用内连接更新语句

我有一个查询在MySQL中正常工作,但是当我在Oracle上运行它时,出现以下错误:

SQL错误:ORA-00933:SQL命令未正确结束
00933. 00000 - “SQL命令未正确结束”

该查询是:

UPDATE table1
INNER JOIN table2 ON table1.value = table2.DESC
SET table1.value = table2.CODE
WHERE table1.UPDATETYPE='blah';

该语法在Oracle中无效。 你可以这样做:

UPDATE table1 SET table1.value = (SELECT table2.CODE
                                  FROM table2 
                                  WHERE table1.value = table2.DESC)
WHERE table1.UPDATETYPE='blah'
AND EXISTS (SELECT table2.CODE
            FROM table2 
            WHERE table1.value = table2.DESC);

或者你可以这样做:

UPDATE 
(SELECT table1.value as OLD, table2.CODE as NEW
 FROM table1
 INNER JOIN table2
 ON table1.value = table2.DESC
 WHERE table1.UPDATETYPE='blah'
) t
SET t.OLD = t.NEW

(这取决于内联视图是否被Oracle视为可更新的)。


Oracle不支持UPDATE语句中的连接。

用这个:

MERGE
INTO    table1 trg
USING   (
        SELECT  t1.rowid AS rid, t2.code
        FROM    table1 t1
        JOIN    table2 t2
        ON      table1.value = table2.DESC
        WHERE   table1.UPDATETYPE='blah'
        ) src
ON      (trg.rowid = src.rid)
WHEN MATCHED THEN UPDATE
    SET trg.value = code;

合并where子句为我工作:

merge into table1
using table2
on (table1.id = table2.id)
when matched then update set table1.startdate = table2.start_date
where table1.startdate > table2.start_date;

您需要WHERE子句,因为ON子句中引用的列无法更新。

链接地址: http://www.djcxy.com/p/83727.html

上一篇: Update statement with inner join on Oracle

下一篇: Use tnsnames.ora in Oracle SQL Developer