【问题标题】:Oracle Update query with joins带有连接的 Oracle 更新查询
【发布时间】:2015-12-14 02:34:07
【问题描述】:

我知道更新子句不适用于 Oracle 中的联接。

update table1 Pr
set code = (select t2.class_attr_value from table2 t2
where  class_attr_name = 'sample' and Pr.epcclass_id = t2.epcclass_id)

如果有人可以帮助我修改我的查询,我将不胜感激,以免我收到 SQL 命令未正确结束的错误。

【问题讨论】:

  • 向我们展示table1table2 表的结构和数据。为此目的使用SQLFiddle。我看不出有什么问题。

标签: oracle


【解决方案1】:

您的查询对我来说似乎没问题,我刚刚添加了表格别名。您的查询将更新 table1 中的所有记录。你得到什么错误......?

建议,

a) 除非您想要更新所有记录,否则请在查询中添加 where 子句以避免更新所有记录...

b) 如果您得到(ORA-01427:单行子查询返回多于一行),则表示相关子查询(括号内)缺少某些条件,使其每个 epcclass_id 仅获取 1 行。

update table1 Pr
set Pr.code = (select t2.class_attr_value 
               from table2 t2 
               where  t2.class_attr_name = 'sample' 
               and t2.epclass_id = Pr.epcclass_id 
              );

【讨论】:

    【解决方案2】:

    试试这个:

    UPDATE table1 Pr INNER JOIN table2 t2 
    ON t2.class_attr_name = 'sample' AND Pr.epcclass_id = t2.epcclass_id 
    SET Pr.code = t2.class_attr_value
    

    【讨论】:

    • 我收到一个错误 ORA-00971:missing SET 关键字
    • 为了测试,我在 ORACLE SQL DEVELOPER 上运行查询
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2018-03-31
    • 2020-11-29
    • 1970-01-01
    相关资源
    最近更新 更多