Oracle中merge into的使用

来源:这里教程网 时间:2026-02-27 12:22:23 作者:

该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. Oracle 9i 中,使用此命令必须同时指定UPDATE 和INSERT 关

该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. oracle 9i 中,使用此命令必须同时指定update 和insert 关键词,oracle 10g 做了如下改动。
  1,insert 和update是可选的 2,update 和insert 后面可以跟where 子句 3,在on条件中可以使用常量来insert 所有的行到目标表中,不需要连接到源表和目标表 4,update 子句后面可以跟delete 来去除一些不需要的行。
举例:
create table products    
     (     
  product_idinteger,    
  product_name varchar2(60),
  category varchar2(60)
     );     
    insert into products values (1501, 'vivitar 35mm', 'electrncs');    
     insert into products values (1502, 'olympus is50', 'electrncs');    
     insert into products values (1600, 'play gym', 'toys');    
     insert into products values (1601, 'lamaze', 'toys');    
     insert into products values (1666, 'harry potter', 'dvd');    
     commit;  
    create table newproducts    
     (     
  product_idinteger,    
  product_name varchar2(60),
  category varchar2(60)
     );     
    insert into newproducts values (1502, 'olympus camera', 'electrncs');    
     insert into newproducts values (1601, 'lamaze', 'toys');    
     insert into newproducts values (1666, 'harry potter', 'toys');    
     insert into newproducts values (1700, 'wait interface', 'books');    
     commit;  
1,可省略的update 或者insert 
    merge into products p     
  2 using newproducts np
     3 on (p.product_id = np.product_id)    
     4 when matched then   
     5 update 
    6 set p.product_name = np.product_name,    
  7 p.category = np.category;
  使用表newproducts中的product_name 和category字段来更新表products 中相同product_id的product_name 和category.
2,当条件不满足的时候把newproducts表中的数据insert 到表products中。 
merge into products p     
  using newproducts np
      on (p.product_id = np.product_id)    
      when not matched then   
      insert 
     values (np.product_id, np.product_name,    
  np.category);
3,带条件的insert 和update 
  insert 和update 都带有where 字句 
merge into products p     
  using newproducts np
     on (p.product_id = np.product_id)    
     when matched then 
    update 
     set p.product_name = np.product_name,    
  p.category = np.category
     where p.category = 'dvd'   
     when not matched then   
      insert 
     values (np.product_id, np.product_name, np.category)    
     where np.category != 'books'   
  4,无条件的insert
merge into products p    
  using newproducts np
   on (1=0)  
  when not matched then   
    insert 
   values (np.product_id, np.product_name, np.category)    
    where np.category = 'books'  
   5,delete 子句 
  1  merge into products p
  2  using newproducts np
  3  on(p.product_id = np.product_id)
  4  when matched then
  5  update
  6  set p.product_name = np.product_name
  7  delete where category = 'macle1_cate';
  product_id product_name         category
  --------------------------------------- -------------------- --------------------
  1502 macle22              macle2_cate
  1503 macle3                macle2_cate
  1504 macle                  macle1_cate
  1505 macle5                macle5_cate
  1504 中的macle1_cate 满足delete where,但是不满足 on 中的条件,所以没有被删除。!!!!!!重点
  -----------------------------------------------
动机:
  想在oracle中用一条sql语句直接进行insert/update的操作。
说明:
  在进行sql语句编写时,我们经常会遇到大量的同时进行insert/update的语句 ,也就是说当存在记录时,就更新(update),不存在数据时,就插入(insert)。
实战:
  接下来我们有一个任务,有一个表t,有两个字段a,b,我们想在表t中做insert/update,如果存在,则更新t中b的值,如果不存在,则插入一条记录。在microsoft的sql语法中,很简单的一句判断就可以了,sql server中的语法如下:
  if exists(select 1 from t where t.a='1001' ) update t set t.b=2 where t.a='1001' else insert into t(a,b) values('1001',2);
  以上语句表明当t表中如果存在a='1001' 的记录的话,就把b的值设为2,否则就insert一条a='100',b=2的记录到t中。
  但是接下来在oracle中就遇到麻烦了,记得在oracle 9i之后就有一条merge into 的语句可以同时进行insert 和update的吗,merge的语法如下:
merge into table_name alias1 
  using (table|view|sub_query) alias2
on (join condition) 
when matched then 
     update table_name 
     set col1 = col_val1, 
         col2     = col2_val 
when not matched then 
     insert (column_list) values (column_values);  
上面的语法大家应该都容易懂吧,那我们按照以上的逻辑再写一次。
merge into t t1
  using (select a,b from t where t.a='1001') t2
on ( t1.a=t2.a)
when matched then
   update set t1.b =  2 
when not matched then 
   insert (a,b) values('1001',2); 
  以上的语句貌似很对是吧,实际上,,该语句只能进行更新,而无法进行insert,错误在哪里呢?
  其实在oracle中merge语句原先是用来进行整表的更新用的,也就是etl工具比较常用的语法,重点是在using上。
  用中文来解释merge语法,就是:
  在alias2中select出来的数据,每一条都跟alias1进行 on (join condition)的比较,如果匹配,就进行更新的操作(update),如果不匹配,就进行插入操作(insert)。
因此,严格意义上讲,”在一个同时存在insert和update语法的merge语句中,总共insert/update的记录数,就是using语句中alias2的记录数。”
  以上这句话也就很好的解释了在上面写的语句为何只能进行update,而不能进行insert了,因为都select不到数据,如何能进行insert呢:)
  接下来要改成正确的语句就容易多了,如下:
merge into t t1
using (select '1001' as a,2 as b from dual) t2
on ( t1.a=t2.a)
when matched then
   update set t1.b = t2.b
when not matched then 
   insert (a,b) values(t2.a,t2.b); 
查询结果,ok!
注意:
  如果不懂merge语句的原理,merge语句是一条比较危险的语句,特别是在您只想更新一条记录的时候,因为不经意间,你可能就把整表的数据都update了一遍.....汗!!!
  我曾经犯过的一个错误如下所示,大家看出来是什么问题了吗?
merge into t t1
  using (select count(*) cnt from t where t.a='1001') t2
on (t2.cnt>0)
when matched then
   update set t1.b = t2.b
when not matched then 
   insert (a,b) values(t2.a,t2.b);

相关推荐