如何有条件的分步删除数据表中的记录
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开发。