你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2017-12-08] 来源:Linux社区  作者:huangbiquan [字体: ]

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

linux
相关资讯       merge into 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款