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';
I have a query which works fine in MySQL, but when I run it on Oracle I get the following error:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
The query is:
UPDATE table1
INNER JOIN table2 ON table1.value = table2.DESC
SET table1.value = table2.CODE
WHERE table1.UPDATETYPE='blah';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
该语法在 Oracle 中无效。您可以执行此操作:
或者您也许能够执行此操作:
这取决于 Oracle 是否认为内联视图可更新
(第二条语句的可更新取决于列出的一些规则
此处)。
That syntax isn't valid in Oracle. You can do this:
Or you might be able to do this:
It depends if the inline view is considered updateable by Oracle
( To be updatable for the second statement depends on some rules listed
here ).
使用这个:
Use this:
带有
WHERE
子句的MERGE
:您需要
WHERE
子句,因为ON
子句中引用的列无法更新。MERGE
withWHERE
clause:You need the
WHERE
clause because columns referenced in theON
clause cannot be updated.不要使用上面的一些答案。
有些人建议使用嵌套 SELECT,不要这样做,它非常慢。如果您有大量记录需要更新,请使用联接,例如:
请参阅此链接以获取更多详细信息。
http://geekswithblogs.net/WillSmith /archive/2008/06/18/oracle-update-with-join-again.aspx。
另外,请确保您要连接的所有表都有主键。
Do not use some of the answers above.
Some suggest the use of nested SELECT, don't do that, it is excruciatingly slow. If you have lots of records to update, use join, so something like:
See this link for more details.
http://geekswithblogs.net/WillSmith/archive/2008/06/18/oracle-update-with-join-again.aspx.
Also, ensure that there are primary keys on all the tables you are joining.
如此处所示,第一个解决方案的一般语法由托尼·安德鲁斯:
我认为这很有趣,特别是如果您想要更新多个字段。
As indicated here, the general syntax for the first solution proposed by Tony Andrews is :
I think this is interesting especially if you want update more than one field.
甲骨文运行良好
It works fine oracle
以下语法对我有用。
This following syntax works for me.
使用 description 代替 table2 的 desc,
Using description instead of desc for table2,
A和B是别名字段,不需要指向表。
A and B are alias fields, you do not need to point the table.
出于完整性考虑,并且因为我们谈论的是 Oracle,所以这也可以做到这一点:
Just as a matter of completeness, and because we're talking Oracle, this could do it as well:
甲骨文基地在这方面有很好的表现。
https://oracle-base.com/articles/misc/updates-基于查询
从这个链接 - 我使用了上述查询的修改,但它对我不起作用(来自使用 rowid 的 mathguy 的答案)
这里我有两个表:源和目标。它们都有一个共同的 varchar 字段,我将源标识字段 (PK) 添加到 dest 表中。
Oracle base has a good run down on this.
https://oracle-base.com/articles/misc/updates-based-on-queries
From this link - I used a modification of the above query which did not work for me (the answer from mathguy which uses rowid)
Here I have two tables: source and dest. They both have a varchar field in common and I am adding the source identify field (PK) into the dest table.
Oracle Database 23ai 添加了对
update
和delete
中直接联接的支持:Oracle Database 23ai has added support for direct joins in
update
anddelete
: