Oracle Meger into 函数
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 函数的更多相关文章
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- 重写Oracle的wm_concat函数,自定义分隔符、排序
oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我 ...
- Oracle日期时间函数大全
ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...
- Oracle过程及函数的参数模式,In、out、in out模式
Oracle过程及函数的参数模式 In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: ( ...
- oracle的substr函数的用法
oracle的substr函数的用法 取得字符串中指定起始位置和长度的字符串 substr( string, start_position, [ length ] ) 如: substr( ...
- Oracle nvl(),nvl2()函数介绍
NVL函数 Oracle/PLSQL中的一个函数. 格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值, ...
- Oracle LPAD/RPAD函数在处理中文时的注意事项
首先看下Oracle官方对函数的定义: The RPAD function returns an expression, right-padded to a specified length with ...
- oracle wm_concat(column)函数的使用
oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并,如果您对oracle wm_concat(c ...
- Oracle之自定义函数
数据库中函数包含四个部分:声明.返回值.函数体和异常处理. --没有参数的函数 create or replace function get_user return varchar2 is v_use ...
随机推荐
- mysql function
mysql 自定义函数的使用 先查看函数功能是否开启:show variables like '%func%'; 若是未开启则:SET GLOBAL log_bin_trust_function_cr ...
- iOS -- app全局字体设置
方法一: 写一个UILabel(FontExtension)扩展重写initWithFrame(手写代码必走方法)和awakeFromNib(xib必走方法)当然UIButton.UITextView ...
- 为什么HierachyViewer无法连接真机调试
关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个人理解:Hi ...
- sublime正则替换
[^a-zA-Z',=]+ 若文本中只有字母和汉字,则上式可用来匹配非字母的中文
- 细说Redis持久化机制
概述 Redis不仅能够作为缓存来使用,也能够作为内存数据库. Redis作为内存数据库使用时.必需要解决一个问题:数据的持久性.有些将Redis作为缓存使用的场景也需要将缓存的数据持久化到存储介质上 ...
- Docker实战(一):基础命令
# 在ubuntu中安装docker $ sudo apt-get install docker.io # 查看docker的版本信息 $ docker version # 查看安装docker的信息 ...
- POJ3114 有些图缩点/改图/最短路
没想到手感还在~ 不须要又一次建图.仅仅要依据条件改改权值就可以. 还跑k次SPFA~ #include<cstdio> #include<iostream> #include ...
- Java中HashMap遍历的两种方法(转)
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- 【C语言】求两个数中不同的位的个数
//求两个数中不同的位的个数 #include <stdio.h> int count_different(int a, int b) { int count = 0; int c = a ...
- 如何求文件File的字节数
[java]代码库 import java.io.*; /** * 获取文件的字节数 */ class FileInputStreamS { public static voi ...