Combining multiple sql update statements (Oracle)

I am not very good at sql and I have tried several things. What would be the best way to combine these 5 update statements into one statement based on code performance ? It would be a great help. Thank you very much!

the code:

----------------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
+4
source share
3 answers

I think you can write this as:

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
+3
source

You can combine your update requests and use only one request:

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

. , 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
0

Source: https://habr.com/ru/post/1686871/


All Articles