I work in SQL Developer by Oracle. I want attach dates from table 2 into empty column Date in Table 1 only into existed rows/ids. I tried to do it by below code but it doesn't work. It seems easy, but I couldnt find solution.
Table 1 Table 2
ID Date ID Date
33 (null) 33 2021-01-02
22 (null) 22 2019-01-02
100 (null) 100 1999-09-09
200 (null) 200 2005-06-07
44 (null) 44 2010-02-02
999 2009-08-06
insert into table1 (date)
select
t2.date
from table2 t2 where table1.id in (select t2.id from table2);
CodePudding user response:
You want an update statement for existing rows. I’ll assume you haven’t really named your column date
.
Something like:
Update table1 t1
Set t1.date_col = (select t2.date_col from table2 t2 where t1.id = t2.id)
CodePudding user response:
think you might be looking for the Oracle Merge statement. If the row exists update it, if not insert it.
MERGE INTO Table1 a
USING Table2 b
ON (a.id = b.id)
WHEN MATCHED THEN
UPDATE set a.Date = b.Date
WHEN NOT MATCHED THEN
INSERT (id, date)
VALUES (b.id, b.Date);