Oracle(merge)
Posted hyp-1901
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle(merge)相关的知识,希望对你有一定的参考价值。
目的:使用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
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
1 iphone 2000 50 electrical
iphone_x
shanghai
3 oracle 80 3000 book
it
UA
it
UA
PRODUCTID PRODUCTNAME PRODUCTPRICE QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
4 dubbo 80 5000 book
it
UA
it
UA
2 head first java 52 2019 book
tecnology
tecnology
PRODUCTID PRODUCTNAME PRODUCTPRICE QUANTITY CATEGORY
---------- -------------------- ------------ ---------- ----------
DESPERATION
--------------------------------------------------------------------------------
ORIGIN
----------
UA
---------- -------------------- ------------ ---------- ----------
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);
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)的主要内容,如果未能解决你的问题,请参考以下文章