阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

merge into的用法及10g新特性总结

186次阅读
没有评论

共计 2537 个字符,预计需要花费 7 分钟才能阅读完成。

merge into 的作用:
    将源数据 (来源于实际的表, 视图, 子查询) 更新或插入到指定的表中(必须实际存在), 依赖于 on 条件, 好处是避免了多个 insert 和 update 操作。
    merge 是一个目标性明确的操作符, 不允许在一个 merge 语句中对相同的行 insert 或 update 操作。
    这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于 insert+update.
    尤其是在大数据量面前,效率越明显.

具体用法如下:
create table t as select rownum id, a.* from user_objects a;
create table t1 as select rownum id, table_name, cast(‘TABLE’ as varchar2(100)) object_type from user_tables;

select * from t;
select * from t1;

–meger into 的用法
–1. 能获得稳定的行时,使用下列语句
merge into t1 using t on (t.object_name = t1.table_name and t.object_type = t1.object_type)
when matched then update set t1.id = t.id
when not matched then insert values(t.id, t.object_name, t.object_type);

–2. 不能获得稳定行时,使用下列语句
merge into t1
using (select object_name, object_type,  max(id) id from t group by object_name, object_type) t
  on (t.object_name = t1.table_name and t.object_type = t1.object_type)
when matched then update set t1.id = t.id
when not matched then insert values (t.id, t.object_name, t.object_type);

– 值得注意的是: merge 语句中的 update 不能修改用于连接的列,否则会报错.

– 创建测试表和插入模拟数据
create table subs(msid number(9), ms_type char(1), areacode number(3));
create table acct(msid number(9), bill_month number(6), areacode number(3), fee number(8,2) default 0.00);
insert into subs values(905310001,0,001);
insert into subs values(905320001,1,002);
insert into subs values(905330001,2,003);
commit;

–a. 操作的表(目标表): 使用原始数据来源的表,并且制定条件,条件必须有括号
merge into acct a using subs b on (a.msid=b.msid) 
– 当匹配的时候,执行 update 操作,和直接 update 的语法不一样,不需要制定表名
when matched then update set a.areacode=b.areacode 
– 当不匹配的时候,执行 insert 操作,也不需要制定表名,若指定字段插入,则在 insert 后用括号标明,不指定是全部插入
when not matched then insert(msid,bill_month,areacode) values(b.msid,’201005′,b.areacode);

select * from acct;
select * from subs;

truncate table acct;

–merge into 10g 新特性
–1. 单个操作
–(1). 单个 not matched 的时候,只做插入
merge into acct a using subs b on(a.msid=b.msid)
 when not matched then insert(a.msid,a.bill_month,a.areacode) values(b.msid,’201005′,b.areacode);

–(2). 单个 matched 的时候,只做更新操作
merge into acct a using subs b on (a.msid = b.msid)
when matched then
  update set a.areacode = b.areacode;

select * from subs where ms_type=0;
 
–2.merge 操作之后,只有匹配的 update 操作才可以,用 delete where 子句删除目标表中 (操作的表) 满足条件的行。
merge into acct a using subs b on (a.msid = b.msid)
when matched then
  update set a.areacode = b.areacode delete where (b.ms_type != 0)
when not matched then
  insert(msid, bill_month, areacode) values (b.msid, ‘201005’, b.areacode) where b.ms_type = 0;
   
–3. 带上 where,满足条件的插入和更新
merge into acct a using subs b on (a.msid=b.msid)
when matched then
    update set a.areacode=b.areacode where b.ms_type=0
when not matched then
    insert(msid,bill_month,areacode) values(b.msid,’201005′,b.areacode) where b.ms_type=0;

更多 Oracle 相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149252.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计2537字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中