86.Oracle数据库SQL开发之 修改表内存——使用MERGE合并行
欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…\
9i引入了MERGE语句,可以用来将一个表中的行合并到另外一个表中。
查询product_changes表:
store@PDB1> selectproduct_id,product_type_id,name,price from product_changes;
PRODUCT_ID PRODUCT_TYPE_ID NAME PRICE
---------- --------------- ----------------------------------------
1 1 Modern Science 40
2 1 New Chemistry 35
3 1 Supernova 25.99
13 2 Lunar Landing 15.99
14 2 Submarine 15.99
15 2 Airplane 15.99
6 rows selected.
想要将product_changes表中的行合并到products表中,则要执行以下操作:
l 对于两个表中product_id值相同的行,将products表中该行各列的值修改为product_changes表中对应的值。
l 对于product_changes表中的新行来说,需要将这些新行插入到products表中。
store@PDB1> merge into products p usingproduct_changes pc on ( p.product_id=pc.product_id)
when matchedthen
update
set
p.product_type_id=pc.product_type_id,
p.name =pc.name,
p.description = pc.description,
p.price =pc.price
when notmatched then
insert (
p.product_id,p.product_type_id,p.name,p.description,p.price)
values (pc.product_id,pc.product_type_id,pc.name,pc.description,pc.price);
6 rows merged.
其中:
MERGE INTO子句指明了合并操作的目标表。
USING ……ON 子句指定了一个表连接
WHEN MATCHED THEN 子句指定了当一行满足USING……ON子句的条件时要执行的操作。
WHEN NOT MATCHED 子句指定了当一行不满足USING……ON子句的条件时要执行的操作。