简体   繁体   English

更新值来自不同表的多个列-Oracle

[英]Update multiple columns where values are from different tables - Oracle

I need to update multiple columns in the same table. 我需要更新同一表中的多个列。 However, the values I need to update to are coming from different tables. 但是,我需要更新的值来自不同的表。 I currently have the update statements separated. 我目前将更新语句分隔开。 The problem is I have a lot of columns to update and running many update statements is causing many number of locks and performance issues. 问题是我有很多要更新的列,并且运行许多更新语句导致许多锁和性能问题。

Is there any way that I can combine them into one update statement? 有什么方法可以将它们合并为一条更新语句? These are two examples from my list of update statements I have now: 这是我现在拥有的更新语句列表中的两个示例:

  UPDATE table1 t1
     SET t1.col1 = (
            SELECT col7
              FROM table1 t1b, table2 t2
             WHERE t1b.col2 = t2.col2
               AND t1b.col3 = t2.col3
               AND t1b.col2 = t1.col2)
   WHERE t1.col4 = 0
     AND t1.col2 IN (SELECT col2 FROM table2);


  UPDATE table1 t1
     SET t1.col5 = (
            SELECT col6
              FROM table1 t1c, table3 t3
             WHERE t1c.col2 = t3.col2
               AND t1c.col3 = t3.col3
               AND t1c.col2 = t1.col2 )
   WHERE t1.col4 = 0
     AND t1.col2 IN (SELECT col2 FROM table3);

A MERGE should do this: 合并应该执行以下操作:

merge into table1 tg
using (
  SELECT t1.col2, t1.col3, 
         col7 
         col6
  FROM table1 t1
    JOIN table2 t2 ON t1.col2 = t2.col2
                  AND t1.col3 = t2.col3
    JOIN table3 t3 ON t1.col2 = t3.col2
                  AND t1.col3 = t3.col3
) x ON (x.col2 = tg.col2 and x.col3 = tg.col3)
when matched then update
   set col1 = x.col7, 
       col5 = x.col6;

Note that this is slightly different to your statements as it assumes that both join to table2 and table3 are successful. 请注意,这与您的语句略有不同,因为它假定连接到table2和table3均成功。 If that is not the case change the (inner) joins to left (outer) joins. 如果不是这种情况,请更改(内部)联接到左侧(外部)联接。

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

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