合并多个sql更新语句(Oracle)

时间:2017-10-04 10:50:11

标签: sql oracle performance

我不太擅长sql而且我尝试了一些东西。考虑到性能代码,将这5个更新语句组合成单个语句的最佳方法是什么?会是一个很大的帮助。非常感谢你!

代码:

----------------1

Update main_table
set  a = (case
..some code..  end)
where condition_2;

----------------2

Update main_table
set  b = (case
..some code.. end)
where condition_2

----------------3

Update main_table
set  c = (select x from sec_table where conditon_1)
where condition_2

----------------4

Update main_table
set  d = (select y from sec_table where conditon_1)
where condition_2

----------------5

Update main_table
set  e = (select z from sec_table where conditon_1)
where condition_2

3 个答案:

答案 0 :(得分:3)

我认为你可以写成:

update main_table
    set a = (case ..some code.. end), 
        b = (case ..some code.. end), 
        (c, d, e) = (select x, y, z from sec_table where conditon_1)
where condition_2

答案 1 :(得分:2)

您可以合并更新查询并仅使用以下一个查询:

UPDATE main_table
   SET a = (case ..some code.. end) ,
       b = (case ..some code.. end) ... /*the rest of your sets*/
   where /*add your conditions*/

答案 2 :(得分:0)

您只能在一个更新语句中执行该操作。根据你在sec_table上制作的子查询,你甚至可以调整一点。

update main_table set a= (case ..some code.. end), 
                      b= (case ..some code.. end), 
                      c= (select x from sec_table where conditon_1),      
                      d= (select y from sec_table where conditon_1),
                      e= (select z from sec_table where conditon_1) 
where condition_2