【摘要】 计算机等级网权威发布如何有条件的分步删除数据表中的记录,更多如何有条件的分步删除数据表中的记录相关信息请访问计算机等级考试网。 如
计算机等级网权威发布如何有条件的分步删除数据表中的记录,更多如何有条件的分步删除数据表中的记录相关信息请访问计算机等级考试网。如何有条件的分步删除数据表中的记录作者:eygle出处:http://blog.eygle.com日期:February 22, 2005 有时候我们需要分配删除数据表的一些记录,分批提交以减少对于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(@#[email protected]#,@#ID>[email protected]#,@#[email protected]#);" 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 <= :[email protected]# 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(@#[email protected]#); 30 DBMS_OUTPUT.PUT_LINE(@#Totally @#||to_char(n_delete)||@# records [email protected]#); 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(@#[email protected]#,@#object_id >[email protected]#,@#[email protected]#)Finished!Totally 19107 records deleted!PL/SQL procedure successfully completed.