Reputation: 17
I have two tables like this with the name in Table 2 as empty for ID=1. Now if I want to insert Table1 to Table 2 I don't a new row for ID=1.
Table1
----------
ID Name
1 A
2 B
3 C
Table2
----------
ID Name
1
Required Output:
ID Name
1 A
2 B
3 C
Upvotes: 0
Views: 429
Reputation: 142713
Looks like a simple merge
:
SQL> merge into t2
2 using t1
3 on (t1.id = t2.id)
4 when matched then update set t2.name = t1.name
5 when not matched then insert (id, name)
6 values (t1.id, t1.name);
4 rows merged.
SQL> select * From t2 order by id;
ID N
---------- -
1 a
2 b
3 c
4 d
SQL>
Upvotes: 2