通过Oracle中的merge实现根据一张表的内容更新另外张表数据的功能

Linux大全评论863 views阅读模式

最近由于项目需要一直想找个关于Oracle中实现根据一张表的内容更新另外张表数据的功能,在网上找了半天也没找到个合适的方法,虽然看到有人说可以通过存储过程来实现,但觉得那样太麻烦,今在查找同样的功能时发现了Oracle中的merge,于是花了点时间用比较简单的方法实现了这个功能。语法就免了,网上一搜一堆,看具体语句吧,代码也比较简单,通过字面就可以理解了,最简单的版本应是:

  1. merge into test1 t1  
  2. using test2 t2  
  3. on (t1.id = t2.id)  
  4. when matched then  
  5.  update set t1.name = t2.name  
  6. when not matched then  
  7.  insert values(t2.id,t2.name)  
  1. merge into public_field t1  
  2. using (select xx.link_id, yy.save_path  
  3.          from (select *  
  4.                  from public_field x  
  5.                 where x.link_id in (select t.id from scenic t)  
  6.                   and x.ci_logo is null) xx  
  7.          left join (select *  
  8.                      from IMAGE_INFO bb  
  9.                     where bb.id in  
  10.                           (select min(id) id  
  11.                              from IMAGE_INFO a  
  12.                             where a.link_id in (select t.id from scenic t)  
  13.                               and a.link_tab = 'SCENIC'  
  14.                             group by link_id)) yy on xx.link_id = yy.link_id) t2  
  15. on (t1.link_id = t2.link_id)  
  16. when matched then  
  17.   update  
  18.      set t1.ci_logo = t2.save_path  
  19.   --when not matched then  
  20.   --insert values(t2.id,t2.name)  

企鹅博客
  • 本文由 发表于 2019年10月2日 03:11:31
  • 转载请务必保留本文链接:https://www.qieseo.com/185549.html

发表评论