简体   繁体   English

使用 Oracle 中的联接查询进行更新

[英]Update with Join query in Oracle

what is wrong in query?查询有什么问题? (it executes indefinitely) (它无限期地执行)

UPDATE table1 t1 SET (t1.col,t1.Output) = (
  SELECT t2.col, t3.Output + t2.col
  FROM tabl2 t3 
  LEFT JOIN table1 t2 ON t3.Join_Key = t2.Join_Key
  WHERE t2.col is not NULL);

Please, help me.请帮我。

Unless your SELECT subquery returns a single row, your UPDATE statement should fail with the error除非您的SELECT子查询返回单行,否则您的UPDATE语句应该失败并出现错误

ORA-01427: single-row subquery returns more than one row

Generally, whey you have a correlated update, you need some condition that relates rows in the outer table T1 to rows in the inner subquery in order to ensure that the subquery returns a single row.通常,如果您有相关更新,则需要一些条件将外部表T1中的行与内部子查询中的行相关联,以确保子查询返回单行。 That would generally look something like这通常看起来像

UPDATE table1 t1 SET (t1.col,t1.Output) = (
  SELECT t2.col, t3.Output + t2.col
  FROM tabl2 t3 
  LEFT JOIN table1 t2 ON t3.Join_Key = t2.Join_Key
  WHERE t2.col is not NULL
    AND t1.some_key = t2.some_key);

Finally, this UPDATE statement is updating every row in T1 .最后,这个UPDATE语句正在更新T1中的每一行。 Is that what you intend?那是你的意图吗? Or do you only want to update the rows where, for example, you find a match in your subquery?或者您是否只想更新例如在子查询中找到匹配项的行?

Your query does not make a whole lot of sense with the generic table1, table2, and join_key references.您的查询对于通用 table1、table2 和 join_key 引用没有多大意义。

If this is not what you are looking for, it would be helpful to have some sample data to get a better idea of what results you are looking for.如果这不是您要查找的内容,那么拥有一些示例数据会有助于更好地了解您要查找的结果。

update table1 t1
   set t1.col = (select t2.col
                 from table2 t2
                 where  t1.join_key = t2.join_key(+)  
                  and  t1.col is not null),
       t1.output = (select t2.output + t1.col
                    from  table2 t2
                   where  t1.join_key = t2.join_key(+)  
                     and  t1.col is not null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM