oracle插入时,若表中不存在该信息则插入,否则更新主键外的其他信息



--测试数据
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;


[color=green]安全一点的做法[/color]

begin
insert into Table (ID,Value,..) values(1001,'222',...);
commit;
exception
when dup_val_on_idx then
update Table set value = '222' where id = 1001;
commit;
when others then
Rollback;
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值