1. merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入。
  2.  
  3. 其基本语法规则是
  4.  
  5. merge into 目标表 a
  6.  
  7. using 源表 b
  8.  
  9. on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)
  10.  
  11. when matched then update set a.更新字段=b.字段
  12.  
  13. when not macthed then insert into a(字段1,字段2……)values(值1,值2……)
  14.  
  15. 变种写法①,只更新:
  16.  
  17. merge into 目标表 a
  18.  
  19. using 源表 b
  20.  
  21. on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)
  22.  
  23. when matched then update set a.更新字段=b.字段,a.更新字段2=b.字段2……
  24.  
  25. 变种写法②,只插入:
  26.  
  27. merge into 目标表 a
  28.  
  29. using 源表 b
  30.  
  31. on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)
  32.  
  33. when not macthed then insert into a(字段1,字段2……)values(值1,值2……)
  34.  
  35. 注:条件字段不可更新
  36.  
  37. 对于Oracle来说,merge9i新增的语法,在10g进行了一些增强,如下:
  38.  
  39. 测试环境:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
  40.  
  41. ①条件操作:
  42.  
  43. merge into 目标表 a
  44.  
  45. using 源表 b
  46.  
  47. on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)
  48.  
  49. when matched then update set a.更新字段=b.字段 where 限制条件
  50.  
  51. when not macthed then insert into a(字段1,字段2……)values(值1,值2……) where 限制条件
  52.  
  53. 举例:
  54.  
  55. merge into test_merge a
  56. using test b
  57. on(a.no=b.no)
  58. when matched then update set a.no2=b.no2 where a.no<>1
  59. when not matched then insert values(b.no,b.no2) where a.no<>100
  60.  
  61. 当然也支持变种①②的写法
  62.  
  63. ②删除操作
  64.  
  65. merge into 目标表 a
  66.  
  67. using 源表 b
  68.  
  69. on(a.条件字段1=b.条件字段1 and a.条件字段2=b.条件字段2 ……)
  70.  
  71. when matched then update set a.更新字段=b.字段
  72.  
  73. delete where b.字段=xxx
  74.  
  75. 举例:
  76.  
  77. merge into test_merge a
  78. using test b
  79. on(a.no=b.no)
  80. when matched then update set a.no2=b.no2 where a.no<>1
  81. delete
  82. 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. 2016年8月17日 内省(1)18_黑马程序员_使用beanUtils操纵javabean

    8.内省(1):18_黑马程序员_使用beanUtils操纵javabean 1.导入两个包: 2.调用静态方法. 9.泛型 map.entrySet() :取出map集合的键值对组成一个set集合. ...

  2. mvc自定义控件

    //自定义一个DatePicker.cshtml文件@helper Init() { <link href="~/Content/mobiscroll.custom-2.5.0.min ...

  3. python 生成测试报告并发送邮件

    前言: 使用unittest编写自动化测试脚本,执行脚本后可以很方便看到测试用例的执行情况. 但如果想向领导汇报工作,就需要提供更直观的测试报告. 思路: 使用unittest编写测试用例,HTMLT ...

  4. centos7网卡重命名为ethx格式

    参考:https://www.cnblogs.com/zyd112/p/8143464.html CentOS 7 使用 eth0 这样的传统名称,那么在安装启动(pxe)时,按Tab键在下方输入以下 ...

  5. 死磕itchat源码--config.py

    itchat的配置文件,源码: import os, platform # 版本及微信的url,二维码等 VERSION = '1.3.10' BASE_URL = 'https://login.we ...

  6. 【剑指Offer】48、不用加减乘除做加法

      题目描述:   写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号.   解题思路:   本题同样是对发散思维能力的一个考察.首先,我们需要考虑是要求和却不能使用四则运算 ...

  7. C#第十五节课

    函数复习 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System. ...

  8. laravel Job 和事件

    在做项目的时候,一直对Job和Event有个疑惑.感觉两者是相同的东西,搞不清楚两者的区别在哪里!经过一段时间的琢磨和查找了相关的资料,对Job和Event做了一些总结,以便记忆. Job Job既可 ...

  9. Problem 52

    Problem 52 It can be seen that the number, 125874, and its double, 251748, contain exactly the same ...

  10. 关于约束ENABLE NOVALIDATE的一个疑问

    http://www.dbunix.com/?p=188 关于约束ENABLE NOVALIDATE的一个疑问 CREATE TABLE test (id varchar2(12), name var ...