zoukankan      html  css  js  c++  java
  • MERGE语法详解

    MERGE语法详解
    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
    备注:删除动作针对的也是目标表,并且必须在语句最后

  • 相关阅读:
    自适应图形的绘制
    红色椭圆、蓝色正方形、黄色三角形、紫色五角星
    [2020牛客寒假算法基础集训营2]G-判正误
    [2020牛客寒假算法基础集训营1]I-nico和niconiconi
    [2020牛客寒假算法基础集训营1]H-nozomi和字符串
    [2020牛客寒假算法基础集训营1]A-honoka和格点三角形
    约瑟夫环以及其变种集合
    unsign long long 与 long long
    【POJ-3279】Fliptile
    B--Bookshelf 2
  • 原文地址:https://www.cnblogs.com/caomj/p/10751407.html
Copyright © 2011-2022 走看看