码迷,mamicode.com
首页 > 数据库 > 详细

Oracle(merge)

时间:2019-01-28 19:17:46      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:int   values   mat   更新   java   str   acl   对比   bsp   

目的:使用merge完成指定数据引用(包括更新和添加)
 
使用merge之前的productinfo和productinfo1数据:
PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
2          head first java                52       2019 book
tecnology
UA

1          computer                     5000         50 electrical
hp_zhan66pro
shanghai

PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------

3          oracle                         80       3000 book
it
UA

4          dubbo                          80       5000 book
it

PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
UA


SQL> select * from productinfo1;

PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
1          iphone                       2000       3000 electrical
iphone_x
shanghai

3          oracle                         80       1836 book
it
UA

PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------




目的:完成productinfo1表的数据添加和更新(对比productinfo),相同id的quantity更新成productinfo表的,没有的productinfo表中id的数据,全部记录添加到productinfo1表中;

实现语句:

SQL> merge into productinfo1 using productinfo on (productinfo1.productid=productinfo.productid) when matched then update
  2  set productinfo1.quantity=productinfo.quantity when not matched then insert
  3  values (productinfo.productid,productinfo.productname,productinfo.productprice,productinfo.quantity,productinfo.category,productinfo.desperation,productinfo.origin);
4 行已合并。


实现后的productinfo1表:

SQL> select * from productinfo1;
PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
1          iphone                       2000         50 electrical
iphone_x
shanghai
3          oracle                         80       3000 book
it
UA
PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
4          dubbo                          80       5000 book
it
UA
2          head first java                52       2019 book
tecnology
PRODUCTID  PRODUCTNAME          PRODUCTPRICE   QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
UA
 
 
 
 
语句:merge into productinfo1 using productinfo on (productinfo1.productid=productinfo.productid) when matched then update
  2  set productinfo1.quantity=productinfo.quantity when not matched then insert
  3  values (productinfo.productid,productinfo.productname,productinfo.productprice,productinfo.quantity,productinfo.category,productinfo.desperation,productinfo.origin);
 
注意:
1.matched是update,not matched是insert
2.目标表using来源表
3.values后面对应的是来源表的字段,对应字段的数据添加到目标表

Oracle(merge)

标签:int   values   mat   更新   java   str   acl   对比   bsp   

原文地址:https://www.cnblogs.com/hyp-1901/p/10331299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!