Oracle中merge into的使用方法

MergeInto是一种用于数据库批量更新、插入和删除的高级SQL语法,尤其适用于大量数据操作。它允许基于关联条件同时更新目标表、插入新记录或删除现有记录。当匹配条件成立时执行更新或删除,不成立时执行插入,且当源表中无对应数据时,可删除目标表中的记录。MergeInto提供了一种优化大数据处理的方法,可以提高性能并减少锁表时间。
摘要由CSDN通过智能技术生成

从网上参考的,如下资料不能满足,可百度搜索其他merge into的使用方法
一、可以同时从1个或者多个源表对目标表进行更新、插入、删除数据,经常用于操作大量的数据,即对于大批量的数据更新、插入时效率极高

二、语法:

merge into table_name  alias1            --目标表 可以用别名
using (table|view|sub_query) alias2      --数据源表 可以是表、视图、子查询
on (join condition)                      --关联条件
when matched then              --当关联条件成立时 更新,删除,插入的where部分为可选 
  --更新
  update  table_name set  col1=colvalue  where……
  --删除  
  delete from table_name  where  col2=colvalue  where……
--可以只更新不删除 也可以只删除不更新。
--如果更新和删除同时存在,删除的条件一定要在更新的条件内,否则数据不能删除。
when not matched then                --当关联条件不成立时   
  --插入
  insert (col3) values (col3values)  where…… 
 when not matched  by source then      --当源表不存在,目标表存在的数据删除
  delete; 

三、语句讲解:
1、on后面的关联条件成立时,可以update、delete
2、on后面的关联条件不成立时,可以insert
3、当源表中不存在数据,而目标表中存在的数据可以删除

四、注意事项:
1、只会操作“操作表”,源表不会有任何变化。
2、不一定要把update,delete,insert 操作都写全,可以根据实际情况。
3、merge into效率很高,强烈建议使用,尤其是在一次性提交事务中,可以先建一个临时表,更新完后,清空数据,这样update锁表的几率很小了。
4、Merge语句还有一个强大的功能是通过OUTPUT子句,可以将刚刚做过变动的数据进行输出。我们在上面的Merge语句后加入OUTPUT子句。
5、可以使用TOP关键字限制目标表被操作的行,如图8所示。在图2的语句基础上加上了TOP关键字,我们看到只有两行被更新。

五、举例说明:
根据源表t_noentryquery_synchron使用 merge into 方法同步t_noentryquery表中,存在更新,不存在则新增

merge into t_noentryquery a
using (SELECT b.* FROM t_noentryquery_synchron b) b
on (a.fbihid = b.fbihid and a.fbincu = b.fbincu)
when matched then
  update
     set a.fmrdid          = b.fmrdid,
		     a.fname           = b.fname,
         a.fsex            = b.fsex,
         a.fage            = b.fage,
         a.fryzd           = b.fryzd,
         a.fdezd           = b.fdezd,
         a.ficd9           = b.ficd9,
         a.fidate          = b.fidate,
         a.fodate          = b.fodate,
         a.fhdate          = b.fhdate,
         a.fzzysno         = b.fzzysno,
         a.fzzys           = b.fzzys,
         a.fdstype         = b.fdstype,
         a.fzkdocno        = b.fzkdocno,
         a.fzkdoc          = b.fzkdoc,
         a.fzknurno        = b.fzknurno,
         a.fzknur          = b.fzknur,
         a.fihdeptno       = b.fihdeptno,
         a.fihdept         = b.fihdept,
         a.fdeptno         = b.fdeptno,
         a.fdept           = b.fdept,
         a.fsz             = b.fsz,
         a.fyz             = b.fyz,
         a.fzyys           = b.fzyys,
         a.fext1           = b.fext1,
         a.fext2           = b.fext2,
         a.fext3           = b.fext3,
         a.fzyysno         = b.fzyysno,
         a.fryzdname       = b.fryzdname,
         a.fcheck          = b.fcheck,
         a.fcreatedate     = b.fcreatedate,
         a.fbirth          = b.fbirth,
         a.freaudit        = b.freaudit,
         a.fzrys           = b.fzrys,
         a.fzrysno         = b.fzrysno,
         a.qtzdbm1         = b.qtzdbm1,
         a.qtzdbm2         = b.qtzdbm2,
         a.qtzdbm3         = b.qtzdbm3,
         a.qtzdbm4         = b.qtzdbm4,
         a.qtzdbm5         = b.qtzdbm5,
         a.qtzdbm6         = b.qtzdbm6,
         a.qtzdbm7         = b.qtzdbm7,
         a.qtzdbm8         = b.qtzdbm8,
         a.qtzdbm9         = b.qtzdbm9,
         a.qtzdbm10        = b.qtzdbm10,
         a.qtzdbm11        = b.qtzdbm11,
         a.qtzdbm12        = b.qtzdbm12,
         a.qtzdbm13        = b.qtzdbm13,
         a.qtzdbm14        = b.qtzdbm14,
         a.qtzdbm15        = b.qtzdbm15,
         a.qtzdbm16        = b.qtzdbm16,
         a.qtzdbm17        = b.qtzdbm17,
         a.qtzdbm18        = b.qtzdbm18,
         a.qtzdbm19        = b.qtzdbm19,
         a.qtzdbm20        = b.qtzdbm20,
         a.fsumfee         = b.fsumfee,
         a.fisdeath        = b.fisdeath,
         a.fryzdmc         = b.fryzdmc,
         a.ficd9mc         = b.ficd9mc,
         a.fdezdmc         = b.fdezdmc,
         a.synchronousid   = b.synchronousid,
         a.synchronousdate = sysdate,
         a.fhspid          = b.fhspid  
				 where a.fbihid=b.fbihid and a.fbincu=b.fbincu
when not matched then
  insert
    (a.fmrdid,
     a.fbihid,
     a.fbincu,
     a.fname,
     a.fsex,
     a.fage,
     a.fryzd,
     a.fdezd,
     a.ficd9,
     a.fidate,
     a.fodate,
     a.fhdate,
     a.fzzysno,
     a.fzzys,
     a.fdstype,
     a.fzkdocno,
     a.fzkdoc,
     a.fzknurno,
     a.fzknur,
     a.fihdeptno,
     a.fihdept,
     a.fdeptno,
     a.fdept,
     a.fsz,
     a.fyz,
     a.fzyys,
     a.fext1,
     a.fext2,
     a.fext3,
     a.fzyysno,
     a.fryzdname,
     a.fcheck,
     a.fcreatedate,
     a.fbirth,
     a.freaudit,
     a.fzrys,
     a.fzrysno,
     a.qtzdbm1,
     a.qtzdbm2,
     a.qtzdbm3,
     a.qtzdbm4,
     a.qtzdbm5,
     a.qtzdbm6,
     a.qtzdbm7,
     a.qtzdbm8,
     a.qtzdbm9,
     a.qtzdbm10,
     a.qtzdbm11,
     a.qtzdbm12,
     a.qtzdbm13,
     a.qtzdbm14,
     a.qtzdbm15,
     a.qtzdbm16,
     a.qtzdbm17,
     a.qtzdbm18,
     a.qtzdbm19,
     a.qtzdbm20,
     a.fsumfee,
     a.fisdeath,
     a.fryzdmc,
     a.ficd9mc,
     a.fdezdmc,
     a.synchronousid,
     a.synchronousdate,
     a.fhspid)
  values
    (b.fmrdid,
		 b.fbihid,
     b.fbincu,
     b.fname,
     b.fsex,
     b.fage,
     b.fryzd,
     b.fdezd,
     b.ficd9,
     b.fidate,
     b.fodate,
     b.fhdate,
     b.fzzysno,
     b.fzzys,
     b.fdstype,
     b.fzkdocno,
     b.fzkdoc,
     b.fzknurno,
     b.fzknur,
     b.fihdeptno,
     b.fihdept,
     b.fdeptno,
     b.fdept,
     b.fsz,
     b.fyz,
     b.fzyys,
     b.fext1,
     b.fext2,
     b.fext3,
     b.fzyysno,
     b.fryzdname,
     b.fcheck,
     b.fcreatedate,
     b.fbirth,
     b.freaudit,
     b.fzrys,
     b.fzrysno,
     b.qtzdbm1,
     b.qtzdbm2,
     b.qtzdbm3,
     b.qtzdbm4,
     b.qtzdbm5,
     b.qtzdbm6,
     b.qtzdbm7,
     b.qtzdbm8,
     b.qtzdbm9,
     b.qtzdbm10,
     b.qtzdbm11,
     b.qtzdbm12,
     b.qtzdbm13,
     b.qtzdbm14,
     b.qtzdbm15,
     b.qtzdbm16,
     b.qtzdbm17,
     b.qtzdbm18,
     b.qtzdbm19,
     b.qtzdbm20,
     b.fsumfee,
     b.fisdeath,
     b.fryzdmc,
     b.ficd9mc,
     b.fdezdmc,
     b.synchronousid,
     sysdate,
     b.fhspid);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值