美文网首页
oracle merge into常见用法

oracle merge into常见用法

作者: clannadyang | 来源:发表于2020-11-23 16:19 被阅读0次

table_a表与table_b有关联关系

  --将 table_b表的数据【插入|更新】到table_a表中
  ## table_a表与table_b有关联关系
  merge into table_a a 
    using (select b.aid,b.name,b.year from table_b b) c on (a.id=c.aid)
  when matched then /*id已存在更新字段*/
       update set a.year=c.year,a.name=c.name
  when not matched then /*id不存在插入数据*/
       insert(a.id,a.name,a.year) values(c.aid,c.name,c.year); 

不在库中的数据判断是否在table_a表中,存在更新否则插入

merge into table_a a 
  using (select 'uuid1' as aid from dual) c on (a.id=c.aid)
when matched then/*id已存在更新字段*/
     update set a.year= 19, a.name='tom'
when not matched then /*id不存在插入数据*/
     insert(a.id,a.name,a.year) values('uuid1','tom',19); 

————————————— or—————————————

merge into table_a a 
  using (select 'uuid1' as aid ,'tom' as name, 19 as year from dual) c on (a.id=c.aid)
when matched then/*id已存在更新字段*/
     update set a.year= c.year, a.name=c.name
when not matched then /*id不存在插入数据*/
     insert(a.id,a.name,a.year) values(c.aid,c.name,c.year); 

使用虚表merge into 虚表需要内置

--*** 正确写法
 merge into table_a a 
  using (
  --虚表 temp_table
  with temp_table as (
       select ta.id as aid,ta.name as name,tb.year as year
       from table_a ta
       left join table_b tb on ta.idcard = tb.idcard  
  )
  --☆☆☆
 select * from temp_table
  ) c on (a.id=c.aid)
when matched then .........
when not matched then .........

--*** 错误写法写法
with temp_table as (
     select ta.id as aid,ta.name as name,tb.year as year
     from table_a ta
     left join table_b tb on ta.idcard = tb.idcard  
) 
merge into table_a a 
  using (select * from temp_table) c on (a.id=c.aid)
when matched then .........
when not matched then .........

merge into匹配条件可单独存在,如下

--只更新
merge into table_a a 
  using (select 'uuid1' as aid from dual) c on (a.id=c.aid)
when matched then 
    update set a.year= 19,a.name='tom'
--只插入
merge into table_a a 
  using (select 'uuid1' as aid from dual) c on (a.id=c.aid)
when not matched then 
   insert(a.id,a.name,a.year) values('uuid1','tom',19); 

相关文章

  • oracle merge into常见用法

    table_a表与table_b有关联关系 不在库中的数据判断是否在table_a表中,存在更新否则插入 使用虚表...

  • oracle Merge Into 用法

    在开发中我们经常会碰到这么一个场景,列如用户修改简单的个人基本信息,这个时候就需要判断用户的基本信息是否存在,如果...

  • Oracle中Merge into用法总结

    有一个表T,有两个字段a、b,我们想在表T中做Insert/Update,如果条件满足,则更新T中b的值,否则在T...

  • Oracle中的Merge into的用法

    在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录时,就更...

  • Oracle SQL中的Merge用法

    Merge语句可以用来插入或更新数据,取决于数据是否已经存在。 语法结构: 例子:新建两个表:customer,c...

  • Oracle高级语句书目录

    Oracle高级语句之with as Oracle高级语句之merge into Oracle高级语句之row_n...

  • [Oracle]中的循环用法

    [Oracle]中的GOTO用法[sql] ORACLE中的FOR循环用法[sql] ORACLE中的WHILE循...

  • Oracle:WITH AS () Merge ?

    WITH AS 语法在SQL SERVER 和ORACLE数据库上均支持,主要用于子查询。语法如下: 但其语句在两...

  • oracle merge into

    场景是,别人给我传一张表中的数据list,其中的数据有些是数据库中有的,有些是新的,已经有的我要更新,新的我要插入...

  • Oracle Merge into 详细介绍

    Merge into 详细介绍 MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句...

网友评论

      本文标题:oracle merge into常见用法

      本文链接:https://www.haomeiwen.com/subject/idlviktx.html