zoukankan      html  css  js  c++  java
  • oracle update 分次提交

    这里我插入1999条记录,然后把记录中xxx为非33的全部更新为33,分20次提交,1次提交100条,比较下更新前后xxx为33的记录个数就知道对不对了

    SQL> CREATE TABLE test (ID VARCHAR2(20),xxx NUMBER);

    Table created

    SQL> INSERT INTO test SELECT lpad(ROWNUM,4,'0'),mod(ROWNUM,34) FROM dual CONNECT BY LEVEL < 2000;

    1999 rows inserted

    Oracle lpad函数将左边的字符串填充一些特定的字符,其语法格式如下:
    lpad( string1, padded_length, [ pad_string ] )
    其中string1是需要粘贴字符的字符串
    padded_length是返回的字符串的数量,如果这个数量比原字符串的长度要短,lpad函数将会把字符串截取成padded_length;
    pad_string是个可选参数,这个字符串是要粘贴到string1的左边,如果这个参数未写,lpad函数将会在string1的左边粘贴空格。

    例如:

    lpad('tech', 7); 将返回' tech'

    lpad('tech', 2); 将返回'te'

    lpad('tech', 8, '0'); 将返回'0000tech'

    lpad('tech on the net', 15, 'z'); 将返回 'tech on the net'

    lpad('tech on the net', 16, 'z'); 将返回 'ztech on the net'

    SQL> commit;

    Commit complete

    SQL> set serverout on;
    SQL> select count(*) from test where id = 33;

    COUNT(*)
    ----------
    1

    SQL> select count(*) from test where xxx = 33;

    COUNT(*)
    ----------
    58

    SQL> select count(*) from test where xxx <> 33;

    COUNT(*)
    ----------
    1941

    SQL>
    SQL> declare
    2 -- 数组变量,保存查询条件
    3 TYPE t_id IS TABLE OF test.id%TYPE;
    4 v_t_id t_id;
    5
    6 CURSOR c IS
    7 SELECT id FROM test where xxx <> 33;
    8 -- 循环次数
    9 cnt NUMBER := 0;
    10 BEGIN
    11 OPEN c;
    12 LOOP
    13 cnt := cnt + 1;
    14 -- 批量更新,一次更新100条数据 ,取出数据放到变量v_t_id中
    15 fetch c bulk collect into v_t_id LIMIT 100;
    16 -- 这里用forall效率更高
    17 FORALL i IN 1 .. v_t_id.COUNT
    18 UPDATE test SET xxx = 33 WHERE id = v_t_id(i);
    19
    20 -- 提交
    21 COMMIT;
    22 -- 循环退出
    23 exit when c%NOTFOUND;
    24
    25 END LOOP;
    26
    27 dbms_output.put_line('循环次数:' || cnt);
    28
    29 CLOSE c;
    30 COMMIT;
    31 end;
    32 /
    循环次数:20

    PL/SQL procedure successfully completed

    SQL> select count(*) from test where xxx = 33;

    COUNT(*)
    ----------
    1999

    SQL> select count(*) from test where xxx <> 33;

    COUNT(*)
    ----------
    0

    SQL>
  • 相关阅读:
    mybatis中的动态语句中多条件or如何书写
    安装kibana的docker版
    安装elasticsearch的docker版
    git回滚push过的代码
    java中支付宝支付
    05 docker镜像删除
    远程仓库的搭建
    本地git工作流
    创建本地仓库
    git安装
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4400524.html
Copyright © 2011-2022 走看看