您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Oracle 中merge into的使用介绍

2024/5/2 2:51:11发布21次查看
http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5980965 该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. oracle 9i 中,使用此命令必须同时指定update 和insert 关键词,oracle
http://blog.csdn.net/yuzhic/article/details/1896878
http://blog.csdn.net/macle2010/article/details/5980965
该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. oracle 9i 中,使用此命令必须同时指定update 和insert 关键词,oracle 10g 做了如下改动。
1,insert 和update是可选的 2,update 和insert 后面可以跟where 子句 3,在on条件中可以使用常量来insert 所有的行到目标表中,不需要连接到源表和目标表 4,update 子句后面可以跟delete 来去除一些不需要的行。
举例:
create table products   
    (   
    product_id integer,   
    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_id integer,   
    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; 
实施数据的更新:
 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;
使用表newproducts中的product_name 和category字段来更新表products 中相同product_id的product_name 和category
实施数据的差异插入:
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);
当条件不满足的时候把newproducts表中的数据insert 到表products中
带条件的insert
   merge into products p   
   using newproducts np   
   on (1=1)   
   when  not matched then  
    insert  
    values (np.product_id, np.product_name, np.category)   
    where np.category = 'f4' 
带条件的 insert 和update
   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 = 'f3'  
    when not matched then  
     insert  
     values (np.product_id, np.product_name, np.category)   
    where np.category != 'f4'
无条件的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 = 'f1'  ;
delete 子句;
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
delete where category = 'q1';
 delete 操作必须满足on 条件时才能执行where字句删除products表中数据的操作。
使用此语法之前,请一定要进行测试。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product