zoukankan      html  css  js  c++  java
  • merge into的用法及10g新特性总结

    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;
  • 相关阅读:
    用 VMControl 管理 IBM i,第 1 部分: VMControl 简介
    使用 VMControl 2.4 实现多网络的 Power 服务器捕捉和系统部署
    lftp 4.4.0 发布,命令行的FTP工具
    Percona Toolkit 2.1.4 发布
    RabbitMQ 2.8.7 发布,AMQP 消息队列
    编程获取本机IPv4及IPv6地址
    Mac下android环境搭建
    Python 3.3.0 RC3 发布
    Sean Lynch谈Facebook Claspin监控工具的由来
    .NET开发者可以在Windows 8中使用ARM
  • 原文地址:https://www.cnblogs.com/huangbiquan/p/8001747.html
Copyright © 2011-2022 走看看