如何有條件的分步刪除數據表中的記錄
2024-07-21 02:05:50
供稿:網友
如何有條件的分步刪除數據表中的記錄作者:eygle出處:http://blog.eygle.com日期:february 22, 2005
« 自己動手,豐衣足食 | blog首頁
有時候我們需要分配刪除數據表的一些記錄,分批提交以減少對于undo的使用,本文提供一個簡單的存儲過程用于實現該邏輯。
你可以根據你的需要進行適當調整,本例僅供參考:
sql> create table test as select * from dba_objects;table created.sql> create or replace procedure deletetab 2 /** 3 ** usage: run the script to create the proc deletetab 4 ** in sql*plus, type "exec deletetab('foo','id>=1000000','3000');" 5 ** to delete the records in the table "foo", commit per 3000 records. 6 ** 7 **/ 8 ( 9 p_tablename in varchar2, -- the tablename which you want to delete from 10 p_condition in varchar2, -- delete condition, such as "id>=100000" 11 p_count in varchar2 -- commit after delete how many records 12 ) 13 as 14 pragma autonomous_transaction; 15 n_delete number:=0; 16 begin 17 while 1=1 loop 18 execute immediate 19 'delete from '||p_tablename||' where '||p_condition||' and rownum <= :rn' 20 using p_count; 21 if sql%notfound then 22 exit; 23 else 24 n_delete:=n_delete + sql%rowcount; 25 end if; 26 commit; 27 end loop; 28 commit; 29 dbms_output.put_line('finished!'); 30 dbms_output.put_line('totally '||to_char(n_delete)||' records deleted!'); 31 end; 32 /procedure created.sql> insert into test select * from dba_objects;6374 rows created.sql> /6374 rows created.sql> /6374 rows created.sql> commit;commit complete.sql> exec deletetab('test','object_id >0','3000')finished!totally 19107 records deleted!pl/sql procedure successfully completed.
很簡單,但是想來也有人可以用到。
,歡迎訪問網頁設計愛好者web開發。