分批插入、分批更新、分批删除

1、分批UPDATE
DROP TABLE T2;
CREATE TABLE T2 AS SELECT OBJECT_NAME FROM DBA_OBJECTS;
SELECT * FROM T2;
SELECT COUNT(*) FROM T2;
DECLARE
TYPE RIDARRAY IS TABLE OF ROWID;
TYPE VCARRAY IS TABLE OF T2.OBJECT_NAME%TYPE;
L_RIDS RIDARRAY;
L_NAMES VCARRAY;
CURSOR C IS
SELECT ROWID,
OBJECT_NAME
FROM T2;
BEGIN
OPEN C;
LOOP
FETCH C BULK COLLECT
INTO L_RIDS,
L_NAMES LIMIT 100000;
FORALL I IN 1 .. L_RIDS.COUNT
UPDATE T2
SET OBJECT_NAME = LOWER(L_NAMES(I))
WHERE ROWID = L_RIDS(I);
COMMIT;
EXIT WHEN C%NOTFOUND;
END LOOP;
CLOSE C;
END;
/
2、分批DELETE
DROP TABLE T3;
CREATE TABLE T3 AS SELECT * FROM DBA_OBJECTS;
DECLARE
CURSOR MYCURSOR IS
SELECT ROWID FROM T3 ORDER BY ROWID; --按ROWID排序的CURSOR,删除条件是XXX=XXXX,根据实际情况修改
TYPE ROWID_TABLE_TYPE IS TABLE OF ROWID INDEX BY PLS_INTEGER;
V_ROWID ROWID_TABLE_TYPE;
BEGIN
OPEN MYCURSOR;
LOOP
FETCH MYCURSOR BULK COLLECT
INTO V_ROWID LIMIT 5000; --每次处理5000行,也就是每5000行一提交
EXIT WHEN V_ROWID.COUNT = 0;
FORALL I IN V_ROWID.FIRST .. V_ROWID.LAST
DELETE FROM T3 WHERE ROWID = V_ROWID(I);
COMMIT;
END LOOP;
CLOSE MYCURSOR;
END;
/

3.分批插入

DECLARE
CURSOR MYCURSOR IS
SELECT * FROM T_20160401 ORDER BY ROWID;
TYPE ROWID_TABLE_TYPE IS TABLE OF T_20160401%ROWTYPE;
V_ROWID ROWID_TABLE_TYPE;
BEGIN
OPEN MYCURSOR;
LOOP
FETCH MYCURSOR BULK COLLECT
INTO V_ROWID LIMIT 50000;
EXIT WHEN V_ROWID.COUNT = 0;

FORALL I IN V_ROWID.FIRST .. V_ROWID.LAST
INSERT INTO T_20160401_01 VALUES V_ROWID(I);
COMMIT;
END LOOP;
CLOSE MYCURSOR;
END;
/

posted @ 2019-11-19 09:57  阿西吧li  阅读(705)  评论(0编辑  收藏  举报