服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > 数据库技术 > Oracle技术 > Oracle开发 > 查看文档

用一个存储过程实现分步删除数据表记录

【赛迪网-it技术报道】简介在实际的工作和学习中,我们需要分步删除数据表的一些记录,分批提交用以减少对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.

扫描关注微信公众号