我有两个表:
table1
:(ID,代码,名称)
table2
:( ID,代码,名称)具有相同的列
我想将数据从表1插入表2或更新表2中存在的列(表1.ID =表2.ID)
简单的方法是什么?
不合并
Merge table2 as target
using table1 as source
on
target.id=source.id
When matched
Then
update
set target.id=source.id,
target.name=source.name
When not matched by Target Then
INSERT (id, name) VALUES (id, name);
Merge语句存在一些问题,因此应谨慎使用。
我进一步建议,使用合并作为两个单独的DML语句,如下所示。
insert into table2
select * from table1 t1 where not exists (select 1 from table2 t2 where t2.id=t1.id)
update t2
set
t2.id=t1.id,
t2.name=t1.name
from
table1 t1
join
table2 t2
on t1.id=t2.id
保罗·怀特(Paul White)在其详细回答中陈述的理由..
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句