可能重复:
Oracle: how to UPSERT (update or insert into a table?)
你们可以给我一个关于如何处理以下情况的建议:
Read table 2 column 1
if value says the record exists in table 1
update table 1 record with table 2 record details
else(value says the record does not exist in table 1)
insert table 1 record with table 2 record details
我是Oracle SQL的初学者,如果有更好的方法,请告诉我。我正在考虑使用游标来解决这个问题。
答案 0 :(得分:6)
最简单的答案是使用merge
语句:
MERGE INTO table1 a
USING ( select column1, column2
from table2 ) b
ON ( a.column1 = b.column1 )
WHEN MATCHED THEN
update set a.column2 = b.column2
WHEN NOT MATCHED THEN
insert (a.column1, a.column2)
values (b.column1, b.column2)
简单地说,这需要select
table2
上的所有内容。然后它会根据条件将此查询加入table1
。如果存在“匹配”,则更新,否则插入。
documentation has more information关于您目前不需要的各种其他选项。
答案 1 :(得分:1)