Is there a better way of writing this general type update in Oracle:
UPDATE table1
SET c1 = ( SELECT d1 FROM table2 WHERE table1.id = table2.id ),
c2 = ( SELECT d2 FROM table2 WHERE table1.id = table2.id )
The update above is only an example. The sub-select could be much more complex.
I see other SQL dialects have UPDATE ... SET ... FROM, but this does not seem to be in Oracle.
You can update multiple columns in one go:
drop table t1;
drop table t2;
create table t1 (col1 number, col2 number, col3 number);
create table t2 (col1 number, col2 number, col3 number);
insert into t1 values (1, 10, 100);
insert into t1 values (2, 20, 200);
insert into t2 values (1, 100, 1000);
insert into t2 values (2, 200, 2000);
commit;
update t1
set (col2, col3) = (select col2, col3 from t2 where t2.col1 = t1.col1);
commit;
select * from t1;
COL1 COL2 COL3
---------- ---------- ----------
1 100 1000
2 200 2000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With