Oracle 在 9i 引入了 merge 命令, 通过这个 merge 能够在一个SQL 语句中对一个表同时执行 inserts 和 updates 操作。Merge into 可以实现用 B 表来更新 A 表数据(如果匹配上),如果 A 表中没有,则把 B 表的数据插入 A 表中。

管中窥豹

MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]

举个栗子:

merge into new_products p using old_products op
on (p.product_id = np.product_id)
when matched then
update set p.product_name = op.product_name
when not matched then
insert values(
op.product_id, op.product_name, op.category)

使用 old_products 表中的输入插入 new_products 中,匹配关系为 on 后面的条件字句的内容。when matched then 就是根据匹配关系匹配上了,when not matched then 就是没有匹配上需要做的相应操作。网上的一般资料都显示在做 merge 的时候,这样同样的情况下,merge 的性能是优于同等功能的update/insert 语句的。

在Oracle 10g中MERGE有如下一些改进:

1、UPDATE 或 INSERT 子句是可选的

2、UPDATE 和 INSERT 子句可以加 WHERE 子句

3、UPDATE 子句后面可以跟 DELETE 子句来去除一些不需要的行

UPDATE 或 INSERT 子句是可选的

merge into new_products p using old_products op
on (p.product_id = np.product_id)
when matched then
update set p.product_name = op.product_name

when matched then 和 when not matched then 都是可选则参数,可以根据具体的业务类型来进行数据库操作,而不用拘泥于原有特定的语法。

添加 WHERE 子句

merge into new_products p using old_products op
on (p.product_id = np.product_id)
when matched then
update set p.product_name = op.product_name where op.name like '%co2fe%'
when not matched then
insert values(
op.product_id, op.product_name, op.category) where op.name like '%co2fe%'

在添加了 where 条件之后我们的 update/insert 就会变得更加的灵活,能够满足更多的业务需求。

DELETE 子句来去除一些不需要的行

merge into new_products p using old_products op
on (p.product_id = np.product_id)
when matched then
update set p.product_name = op.product_name
delete where op.name like '%co2fe%'
when not matched then
insert values(
op.product_id, op.product_name, op.category)

同样的,使用 delete 语句之后我们可以实现更多的功能和业务,扩展了 merge into 的使用面。

本文由个人 hexo 博客 co2fe.com 迁移

date: 2017-09-12 15:38:41

Oracle Meger into 函数的更多相关文章

  1. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  2. 重写Oracle的wm_concat函数,自定义分隔符、排序

    oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我 ...

  3. Oracle日期时间函数大全

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...

  4. Oracle过程及函数的参数模式,In、out、in out模式

    Oracle过程及函数的参数模式 In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: ( ...

  5. oracle的substr函数的用法

    oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串   substr( string, start_position, [ length ] ) 如:     substr( ...

  6. Oracle nvl(),nvl2()函数介绍

    NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...

  7. Oracle LPAD/RPAD函数在处理中文时的注意事项

    首先看下Oracle官方对函数的定义: The RPAD function returns an expression, right-padded to a specified length with ...

  8. oracle wm_concat(column)函数的使用

    oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并,如果您对oracle wm_concat(c ...

  9. Oracle之自定义函数

    数据库中函数包含四个部分:声明.返回值.函数体和异常处理. --没有参数的函数 create or replace function get_user return varchar2 is v_use ...

随机推荐

  1. AC日记——背包问题 V2 51nod 1086

    有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里,每种物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi ...

  2. Javascript 评估用户输入密码的强度

    什么是一个安全的密码呢? 1.如果密码少于5位,那么就认为这是一个弱密码. 2.如果密码只由数字.小写字母.大写字母或其它特殊符号当中的一种组成,则认为这是一个弱密码. 3.如果密码由数字.小写字母. ...

  3. tomcat7.0.55配置单向和双向HTTPS连接

    HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书 下面的配置都没有用CA签名来配置,都不能用于生产环境,实际配置中是需要CA的, ...

  4. ML | spectral clustering

    What's xxx In multivariate statistics and the clustering of data, spectral clustering techniques mak ...

  5. Remove Nth Node From End of List(链表,带测试代码)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. AGC006

    AtCoder Grand Contest 006 <br > 心血来潮,开了一套AGC..... 然后发现各种不会做.........感觉智商被AGC摁在地上摩擦...... <b ...

  7. Xcode: This device is no longer connected error

    Quit the xcode and connect again will all right.

  8. Java Unsafe类

    参考了这篇文章:http://blog.csdn.net/aesop_wubo/article/details/7537278 <JAVA并发编程学习笔记之Unsafe类> Unsafe开 ...

  9. ARC forbids Objective-C objects in structs or unions

    解决方法有二种: 1.在出错的地方加入__unsafe_unretained 2.关闭系统ARC.1.点击project   2.点击Build Setting    3.找到其以下的Objetive ...

  10. Data Binding Guide——google官方文档翻译(上)

    android引入MVVM框架时间还不长,眼下还非常少有应用到app中的.但它是比較新的技术,使用它来搭建项目能省非常多代码,并且能使用代码架构比較清晰.本篇文章是我在学习MVVM时翻译的.篇幅比較长 ...