merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入。

其基本语法规则是

merge into 目标表 a

using 源表 b

on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)  

when matched then update set a.更新字段=b.字段

when  not macthed then insert into a(字段1,字段2……)values(值1,值2……)

变种写法①,只更新:

merge into 目标表 a

using 源表 b

on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)  

when matched then update set a.更新字段=b.字段,a.更新字段2=b.字段2……

变种写法②,只插入:

merge into 目标表 a

using 源表 b

on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)  

when  not macthed then insert into a(字段1,字段2……)values(值1,值2……)

注:条件字段不可更新

对于Oracle来说,merge是9i新增的语法,在10g进行了一些增强,如下:

测试环境:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

①条件操作:

merge into 目标表 a

using 源表 b

on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)  

when matched then update set a.更新字段=b.字段  where 限制条件

when  not macthed then insert into a(字段1,字段2……)values(值1,值2……) where 限制条件

举例:

merge into test_merge a
using test b
on(a.no=b.no)
when matched then update set a.no2=b.no2 where a.no<>1
when not matched then insert values(b.no,b.no2) where a.no<>100 当然也支持变种①②的写法 ②删除操作 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……) when matched then update set a.更新字段=b.字段 delete where b.字段=xxx 举例: merge into test_merge a
using test b
on(a.no=b.no)
when matched then update set a.no2=b.no2 where a.no<>1
delete
where b.no=14

merge into语句的使用的更多相关文章

  1. mybatis 使用oracle merge into 语句踩坑实录

    由于需求涉及oracle的clob类型字段,在mybatis的mapper xml文件中编写merge into语句时总是失败. 附上错误代码 <insert id="mergeInt ...

  2. 2.5 Oracle之存储过程和MERGE INTO语句

    一.MERGE INTO语句 1.merge into语句的功能:我们操作数据库的时候,有时候会遇到insert或者Update这种需求.我们操纵代码时至少需要写一个插入语句和更新语句并且还得单独写方 ...

  3. 纪念我人生中第一个merge into语句

    做按组织关系汇总功能时,当数据量特别大,或者汇总组织特别多时,运行效率特别低,于是使用了merge into语句. 代码如下: public void updateInsertData(DataSet ...

  4. Merge into语句用法及其效率问题

    Merge into语句用法及其效率问题 /*Merge into 详细介绍MERGE语句用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, ...

  5. DB2使用MERGE INTO语句实现西虹市首富的新增及更新操作

    首先我们新建一张名为XIHONGSHISHOUFU的表,这张表是评委会初步评选出的西虹市首富的候选人员,下面的SQL语句包含建表和插入数据的部分: CREATE TABLE XIHONGSHISHOU ...

  6. Oracle 使用MERGE INTO 语句更新数据

    /*Merge into 详细介绍MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,连接条件匹配 ...

  7. 《oracle每天一练》Merge Into 语句代替Insert/Update在Oracle中的应用实战

    转载自窃破天道 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也 ...

  8. 使用Merge Into 语句实现 Insert/Update

    网址: http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html 动机: 想在Oracle中用一条SQL语句直接进行Insert ...

  9. Merge Into 语句代替Insert/Update在Oracle中的应用实战

    动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明: 在进行SQL语句编写时,我们经常会遇到大量的同时进行Insert/Update的语句 ,也就是说当存在记录 ...

  10. Oracle merge into 语句进行insert或者update操作,如果存在就update,如果不存在就insert

    merge into的形式:    MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] ...

随机推荐

  1. javascript 基础知识点

    NaN; // NaN表示Not a Number,当无法计算结果时用NaN表示 Infinity; // Infinity表示无限大,当数值超过了JavaScript的Number所能表示的最大值时 ...

  2. brew 安装的.net 运行时提示"Did you mean to run dotnet SDK commands?"

    原因未知,但有解决方案 使用 brew cask 安装的.NET Core brew cask install dotnet 结果运行时出现: 解决方案: 下载官方 .pkg 文件安装,顺便卸载掉 b ...

  3. The remote certificate is invalid according to the validation procedure 远程证书验证无效

    The remote certificate is invalid according to the validation procedure   根据验证过程中远程证书无效 I'm calling ...

  4. linux下怎么退出telnet

    在运维过程中,常常会telnet某个ip端口,如果 能telnet通,怎么退出呢 ? 1.telnet 63.172.25.18 6463 回车 Trying 63.172.25.18... Conn ...

  5. C#使用OracleDataReader返回DataTable

    string data = string.Empty; DataTable OutDataTable = new DataTable(); OracleDataReader daReader = cm ...

  6. PAT_A1149#Dangerous Goods Packaging

    Source: PAT A1149 Dangerous Goods Packaging (25 分) Description: When shipping goods with containers, ...

  7. Ubuntu安装RTX2080显卡驱动

    安装RTX2080显卡驱动 近日新购了一台DELL服务器,用于TensorFlow,由于显卡是另加的,需要安装显卡驱动. 服务器配置 服务器型号:DELL PowerEdge R730 CPU:2*I ...

  8. Java8自定义条件让集合分组

    /** * 将一个指定类型对象的集合按照自定义的一个操作分组: 每组对应一个List.最终返回结果类型是:List<List<T>> * * @param <T> ...

  9. HDU-1134 卡特兰数+java大数模板

    题意: 给你一个n,然后1,2,3...2n-1,2n围一圈,让每个数都能用一条线配对并且线与线之间不能交叉,问有几种方法数. 思路: 1 可以和2,4,6...连接.假如   一共有8个数,1和2连 ...

  10. pause、jobs、setitimer(2)、system v ipc(day12)

    一.pause()的使用 #include <unistd.h> int pause(void); 功能:等待信号的到来 返回值: - 错误 errno被设置 只有在信号处理函数执行完毕的 ...