zoukankan      html  css  js  c++  java
  • oracle中更新关键字merge和 with as关键字

    merge是oracle特有的语句,两表关联操作(增、删、改)就效率非常高

    merge into table_name alias1 
    using (table|view|sub_query) alias2
    on (join condition) 
    when matched then 
        update table_name 
        set col1 = col_val1,
    col2 = col2_val
    when not matched then insert ( column_list ) values (
        column_values
    );

    它的原理是在alias2中Select出来的数据,每一条都跟alias1进行 ON (join condition)的比较,如果匹配,就进行更新的操作(Update),如果不匹配,就进行插入操作(Insert)。执行merge不会返回影响的行数。Merge语句的写法比较繁琐,并且最多只能两个表关联,复杂的语句用merge更新法将力不从心且效率差。

    也可以使用快速游标更新法

    begin
    for cr in (查询语句) loop –-循环
       --更新语句(根据查询出来的结果集合)
    endloop; --结束循环
    end;

    oracle支持快速游标,不需要定义直接把游标写到for循环中,这样就方便了我们批量更新数据。再加上oracle的rowid物理字段(oracle默认给每个表都有rowid这个字段,并且是唯一索引),可以快速定位到要更新的记录上。

    with as 

    with
    cr as
    (
        select CountryRegionCode from person.CountryRegion where Name like 'C%'
    )
    select * from person.CountryRegion  -- 应将这条SQL语句去掉
    -- 使用CTE的SQL语句应紧跟在相关的CTE后面 --
    select * from person.StateProvince where CountryRegionCode in (select * from cr)

    这里可以将cr看做是一个视图名。十分方便平时在工作中进行操作。

  • 相关阅读:
    JS中的继承(上)
    一篇文章理解JS继承——原型链/构造函数/组合/原型式/寄生式/寄生组合/Class extends
    JS 装饰器,一篇就够
    理解 JavaScript 的 async/await
    JS正则表达式入门,看这篇就够了
    JavaScript的几种循环方式
    全解跨域请求处理办法
    下班后的时间精力生活管理办法(时间管理)
    hexo上部署博客到Github失败
    11
  • 原文地址:https://www.cnblogs.com/dqcer/p/9804126.html
Copyright © 2011-2022 走看看