這里我插入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>
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>
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
