在 Oracle 上使用内部联接更新语句

2021-12-01 00:00:00 sql oracle inner-join ora-00933

我有一个在 MySQL 中运行良好的查询,但是当我在 Oracle 上运行它时出现以下错误:

I have a query which works fine in MySQL, but when I run it on Oracle I get the following error:

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

SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"

查询是:

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

推荐答案

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

That syntax isn't valid in Oracle. You can do this:

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);

或者你可能能够做到这一点:

Or you might be able to do this:

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 是否认为内联视图可更新(要更新第二个语句取决于列出的一些规则此处).

It depends if the inline view is considered updateable by Oracle ( To be updatable for the second statement depends on some rules listed here ).

相关文章