批量提交(plsql,游標)

2021-09-30 16:46:35 字數 1582 閱讀 2543

需求:批量提交修改資料,其中需要修改的資料有 23萬多條。

declare

cursor l_c1 is select u.id from 系統名(可不寫).表名 u where 條件; //定義游標

type t1 is table of pls_integer;

l_t1 t1;

begin

open l_c1;

loop

fetch l_c1 bulk collect into l_t1 limit 1000; //限制一次提交的數量

forall i in 1..l_t1.count

update 系統名.表名t set t.欄位="值" where t.id = l_t1(i);

commit;

exit when l_t1.count=0;

end loop;

close l_c1;

commit;

end;

例項:

declare

cursor l_c1 is select u.id from ttt.tbl_base u where u.name='7';

type t1 is table of pls_integer;

l_t1 t1;

begin

open l_c1;

loop

fetch l_c1 bulk collect into l_t1 limit 1000;

forall i in 1..l_t1.count

update ttt.tbl t set t.value= 'spe01' where t.id = l_t1(i);

commit;

exit when l_t1.count=0;

end loop;

close l_c1;

commit;

end;

備註:

dba在生產庫之後,會報錯:ora-01722。

dba以上例項進行了修改:

declare

cursor l_c1 is select u.id from ttt.tbl_base u where u.name='7';

type t1 is table of ttt.tbl_base .id%type; //字段型別問題(修改了這裡)。

//資料庫的字段id的型別是 varchar2

l_t1 t1;

begin

open l_c1;

loop

fetch l_c1 bulk collect into l_t1 limit 1000;

forall i in 1..l_t1.count

update ttt.tbl t set t.value= 'spe01' where t.id = l_t1(i);

commit;

exit when l_t1.count=0;

end loop;

close l_c1;

commit;

end;

pl sql游標 PL SQL游標 1

pl sql游標 游標 隱式游標 sql返回單行。由oracle server建立。顯式游標 sql重新調整多個記錄行。由使用者建立。游標生命週期 宣告 開啟 獲取 檢查最後一條記錄 關閉 基本語法 declare cursor cursorname param1,param2,is select ...

PL SQL 游標變數

start 游標變數非常有用,游標變數可以在不同的儲存過程中傳遞,也可以返回給客戶端。create table student id int not null,name varchar2 30 not null,class varchar2 10 insert into student values...

PL SQL語法 游標

oracle中的游標分為顯式游標和隱式游標。隱式游標是系統自動為你建立的。顯式游標是使用者通過建立cursor的方式來建立。在oracle中有三種型別的游標 1 不帶引數的游標 eg cursor customer cur is select from customer s 2 帶引數的游標 eg ...