加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Merge into函数用法详解及示例

(2017-03-28 09:26:32)
标签:

mergeinto用法详解

oracle中merge用法

oracle中merge示例

mergeinto示例

mergeinto用法

分类: Oracle数据库
       Oracle在9i中引入了merge命令,通过这个 merge into 语句,能够在一个SQL语句中对一个表同时执行update和insert操作。当然是update还是insert得依据于你的指定的条件判断的,merge into 可以实现用B表来更新A表数据,如果A表中有匹配的记录,则更新数据;没有匹配的记录,则把B表的记录插入A表。merge 命令可以从一个或多个数据源中选择行来update或insert到一个或多个表中。
一、merge函数语法
语法如下:
merge into [your table-name][rename your table here]    
using ([write your query here] )[rename your query-sql and using just like a table]  
on ([condition here] and others condition)  
when mathed then 
  [here you can execute some update sql or something else ] 
when not mathed then
  [execute something else here] 



二、在Oracle9i中使用示例
1、a表和b表比对匹配,进行更新和插入数据操作
merge into tmp_prod a 
using tmp_prod_new b 
on (a.product_id = b.product_id)  
when matched then  
   update set a.product_name = b.product_name  
when not matched then   
   insert values(b.product_id, b.product_name, b.category)

示例说明:
       前面的 merge into tmp_prod using tmp_prod_new 表示用 tmp_prod_new 表来merge到tmp_prod表,merge的匹配关系就是on后面的条件子句的内容,这里根据两个表的product_id来进行匹配,那么匹配上了我们的操作是就是when matched then的子句里的动作了,这里的动作是
update set a.product_name = b.product_name, 很显然就是把newproduct里的内容,赋值到product的product_name里。
      如果不匹配,则insert这样的一条记录到tmp_prod 表中,大家看看这个 merge into 的用法是不是一目了然了。
      这里merge的功能,就像比较,然后选择更新或是插入,是一系列的组合动作,在做merge的时候,与其他更新添加操作函数相比,merge的性能是优于同等功能的update和insert语句的。

2、b表采用视图或子查询
我们也可以在using后面使用视图或者子查询。比如我们把tmp_prod_new换成(select * from tmp_prod_new),也是可以的。

merge into tmp_prod a 
using (select * from tmp_prod_new) b 
on (a.product_id = b.product_id) 
when matched then
  update set a.product_name = b.product_name  
when not matched then
  insert values(b.product_id, b.product_name, b.category)  

三、在Oracle 10g中特性及示例
在Oracle 10g中merge有如下一些改进:  
1.update或insert子句是可选的;  
2.update和insert子句可以加where子句;
3.在on条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表;
4.update子句后面可以跟delete子句来去除一些不需要的行。
  
我们通过实例,来看看如上的新特性:
1、update或insert子句是可选的
       在9i里由于必须insert into和update都要存在,也就是不是update就是insert,不支持单一的操作;而10g里就是可选了,能符合我们更多的需求了,比如上面的句子,我们可以只存在这里,如果匹配就更新,不存在就不管了。   
merge into tmp_prod a 
using tmp_prod_new b 
on (a.product_id = b.product_id)  
when matched then 
  update set a.product_name = b.product_name   
  
2、update和insert子句可以加where子句
       这也是一个功能性的改进,能够符合我们更多的需求,这个where的作用很明显是一个过滤的条件,是我们加入一些额外的条件,只对满足where条件的进行更新和insert 。
merge into tmp_prod a 
using (select * from tmp_prod_new) b 
on (a.product_id = b.product_id)  
when matched then   
  update set a.product_name = b.product_name 
where b.product_name='book'

示例说明: 
       这里表示只是对product_name是'book'的匹配上的进行update,如果不是'book'的就是匹配了也不做什么事情,insert里也可以加入where,比如:  
merge into tmp_prod a 
using (select * from tmp_prod_new) b 
on (a.product_id = b.product_id)  
when matched then  
  update set a.product_name = b.product_name 
  where b.product_name ='book
when not matched then 
  insert values(b.product_id, b.product_name, b.category) 
  where b.product_name ='book'

这里注意比较一下,他们返回的结果行数,是有着差异的。

3、在on条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表    
merge into tmp_prod a 
using (select * from tmp_prod_new) b 
on (1=0)  
when matched then  
  update set a.product_name = b.product_name  
when not matched then   
  insert values(b.product_id, b.product_name, b.category)   

个人觉得这个功能意义不大,我们的insert into本身就支持这样的功能,没有必要使用merge。    

4、update子句后面可以跟delete子句来删除一些不需要的记录行
delete只能和update配合,从而达到删除满足where条件的子句的纪录
merge into tmp_prod a 
using (select * from tmp_prod_new) b 
on (a.product_id = b.product_id)  
when matched then   
  update set a.product_name = b.product_name 
  delete where a.product_id = b.product_id 
  where b.product_name ='book'
when not matched then 
  insert values(b.product_id, b.product_name, b.category)   

示例说明:  
这里我们达到的目的就是会把匹配的记录的prodcut_name更新到product里,并且把product_name中为'book'的删除掉。

四、总结
merge into 也是一个dml语句,和其他的dml语句一样需要通过rollback和commit 结束事务。
merge是一个非常强大的函数,在我们的日常的数据处理需求中经常会用到,希望大家在以后的工作中熟能生巧。



本文参考资料:https://wenku.baidu.com/view/e6f63d15964bcf84b9d57bcc.html

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有