--测试数据
create table table1(id varchar2(100),name varchar2(1000),address varchar2(1000));
insert into table1(id,name,address)values('01001','影子','河北') ;
commit;
--插入
merge into table1 t1
using (select '01002' id,'影子' name,'河北' address from dual) t2
on (t1.id = t2.id)
when matched then
update set t1.name = t2.name, t1.address = t2.address
when not matched then
insert values (t2.id, t2.name,t2.address);
commit;
--查询结果
select * from table1
01001 影子 河北
01002 影子2 辽宁
--更新
merge into table1 t1
using (select '01001' id,'不是影子' name,'山西' address from dual) t2
on (t1.id = t2.id)
when matched then
update set t1.name = t2.name, t1.address = t2.address
when not matched then
insert values (t2.id, t2.name,t2.address);
commit;
--查询结果
select * from table1
01001 不是影子 山西
01002 影子2 辽宁
--删除测试数据
drop table table1;
Oracle 用merge 实现对一张表的操作存在则修改不存在则插入
最新推荐文章于 2025-03-28 14:44:49 发布