oracle 大批量刪除資料分批commit
一切以程式碼說話,
CREATE OR REPLACE PROCEDURE prc_del_big_rec(i_tablename IN VARCHAR2,
i_condition IN VARCHAR2,
i_count IN VARCHAR2) AS
PRAGMA AUTONOMOUS_TRANSACTION;
v_delete_rows NUMBER := 0;
BEGIN
WHILE 1 = 1 LOOP
EXECUTE IMMEDIATE 'delete from ' || i_tablename || ' where ' ||
i_condition || ' and rownum <= :rn'
USING i_count;
IF SQL%NOTFOUND THEN
EXIT;
ELSE
v_delete_rows := v_delete_rows + SQL%ROWCOUNT;
END IF;
COMMIT;
END LOOP;
COMMIT;
prc_running_log('prc_del_big_rec',
SYSDATE,
'Finished, Totally ' || to_char(v_delete_rows) ||
' records deleted',
NULL);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
prc_running_log('prc_del_big_rec',
SYSDATE,
'異常回滾',
substr(SQLERRM, 1, 255));
RAISE;
END;
--相關聯儲存過程
CREATE OR REPLACE PROCEDURE prc_running_log(i_prc_name VARCHAR2, --程式名稱
i_date DATE, --記錄執行時間,一般為當前系統時間
i_running_info VARCHAR2, --執行日誌
i_errm_remark VARCHAR2 --執行錯誤資訊或其他備註
) IS
--記錄執行日誌及錯誤資訊
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
--賦值執行的程式名稱
INSERT INTO tbl_running_log_info
VALUES
(tbl_running_log_info_seq.nextval,
i_prc_name,
i_date,
i_running_info,
i_errm_remark);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END prc_running_log;
--執行刪除儲存過程
declare
v_last_month varchar2(8);
begin
v_last_month :='201805';
prc_del_big_tbl(i_tablename => 'tmp_wrh_test',
i_condition => 'substr(sum_date,1,6) =' ||
v_last_month,
i_count => 1000);
end;