DIY编程器网

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 894|回复: 0
打印 上一主题 下一主题

[待整理] 教你快速掌握分别删除数据表记录的方法

[复制链接]
跳转到指定楼层
楼主
发表于 2014-10-13 14:15:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
很多情况下我们需要分别删除数据表的一些记录,分批来提交以此来减少对于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 ** Condition with default value '1=1' and default Commit batch is 10000.

7 **/

8 (

9 p_TableName in varchar2, -- The TableName which you want to delete from

10 p_Condition in varchar2 default '1=1', -- Delete condition, such as "id>=100000"

11 p_Count in varchar2 default '10000' -- 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.


注释:在此实例中修正了一下,增加了2个缺省值,以下是具体过程:


create or replace procedure deleteTab

(

p_TableName in varchar2,

-- The TableName which you want to delete from

p_Condition in varchar2 default '1=1',

-- Delete condition, such as "id>=100000"

p_Count in varchar2 default '10000'

-- Commit after delete How many records

)

as

pragma autonomous_transaction;

n_delete number:=0;

begin

while 1=1 loop

EXECUTE IMMEDIATE

'delete from '||p_TableName||'

where '||p_Condition||' and rownum <= :rn'

USING p_Count;

if SQL%NOTFOUND then

exit;

else

n_delete:=n_delete + SQL%ROWCOUNT;

end if;

commit;

end loop;

commit;

DBMS_OUTPUT.PUT_LINE('Finished!');

DBMS_OUTPUT.PUT_LINE('Totally '||to_char(n_delete)||' records deleted!');
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|文字版|手机版|DIY编程器网 ( 桂ICP备14005565号-1 )

GMT+8, 2024-11-16 17:28 , 耗时 0.085097 秒, 18 个查询请求 , Gzip 开启.

各位嘉宾言论仅代表个人观点,非属DIY编程器网立场。

桂公网安备 45031202000115号

DIY编程器群(超员):41210778 DIY编程器

DIY编程器群1(满员):3044634 DIY编程器1

diy编程器群2:551025008 diy编程器群2

QQ:28000622;Email:libyoufer@sina.com

本站由桂林市临桂区技兴电子商务经营部独家赞助。旨在技术交流,请自觉遵守国家法律法规,一旦发现将做封号删号处理。

快速回复 返回顶部 返回列表