Oracle merge into 的用法详解实例

作用:merge into 解决用B表跟新A表数据,如果A表中没有,则把B表的数据插入A表;

--  语法:

--  MERGE INTO [your table-name] [rename your table here]

--  USING ( [write your query here] )[rename your query-sql and using just like a table]

--  ON ([conditional expression here] AND [...]...)

--  WHEN MATHED THEN [here you can execute some update sql or something else ]

--  WHEN NOT MATHED THEN [execute something else here ! ]

-------实例-------

drop table a;
drop table b;
create table a(id number(1),name varchar2(10));
create table b(id number(1),name varchar2(10));
insert into a values(1,'2');
insert into b values(1,'1');
insert into b values(2,'2');
commit;

merge into  a
using (select id,name from b ) c
on(a.id=c.id )
when not matched then insert (a.id,a.name) values (c.id,c.name);


merge into  a
using (select id,name from b ) c
on(a.id=c.id )
when matched then update set a.name=c.name;

merge into  a
using (select id,name from b ) c
on(a.id=c.id )
when matched then update set a.name=c.name
when not matched then insert (a.id,a.name) values (c.id,c.name);


作用:将表 b 数据 更新到 表a ,条件是a.id=b.id,如果a表中有该条件的数据就修改,没有该条件的数据就插入。

如果你的数据量很大,此sql效率非常高。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值