Update rows in one table with data from another table based on one column in each being equal Update rows in one table with data from another table based on one column in each being equal oracle oracle

Update rows in one table with data from another table based on one column in each being equal


update   table1 t1set  (    t1.column1,     t1.column2      ) = (    select      t2.column1,       t2.column2    from      table2  t2    where      t2.column1 = t1.column1     )    where exists (      select         null      from         table2 t2      where         t2.column1 = t1.column1      );

Or this (if t2.column1 <=> t1.column1 are many to one and anyone of them is good):

update   table1 t1set  (    t1.column1,     t1.column2      ) = (    select      t2.column1,       t2.column2    from      table2  t2    where      t2.column1 = t1.column1    and      rownum = 1         )    where exists (      select         null      from         table2 t2      where         t2.column1 = t1.column1      ); 


If you want to update matching rows in t1 with data from t2 then:

update t1set (c1, c2, c3) = (select c1, c2, c3 from t2 where t2.user_id = t1.user_id)where exists(select * from t2 where t2.user_id = t1.user_id)

The "where exists" part it to prevent updating the t1 columns to null where no match exists.


merge into t2 t2 using (select * from t1) t1on (t2.user_id = t1.user_id)when matched then updateset   t2.c1 = t1.c1,  t2.c2 = t1.c2