oracle存储过程多分支怎样写,如何写一个分段提交的存储过程啊

以下是我做批量删除的时候用到的几种方法,删除和UPDATE,INSERT差不多,可以依葫芦画瓢,所以我就不改了,LZ自己看看吧

create or replace procedure delete_table

is

i number(10);

begin

for x in (select * from emp where DEPTNO like 'a%')

loop

delete emp where emp.id = x.id

i:=i+1;

if i>1000 then

commit;

i:=0;

end if;

end loop;

exception

when others then

dbms_out.put_line(sqlcode);

rollback;

end delete_table;

或者

DECLARE

CNT NUMBER(10):=0;

I NUMBER(10);

BEGIN

SELECT COUNT(*) INTO CNT FROM ep_arrearage_bak WHERE TO_CHAR(DF_DATE,'MM')='01';

FOR I IN 1..TRUNC(CNT/500)+1 LOOP

DELETE FROM ep_arrearage_bak WHERE TO_CHAR(DF_DATE,'MM')='01' AND ROWNUM<=500;

COMMIT;

END LOOP;

END;

或者

有条件的分步删除数据表中的记录

--创建测试表

create table test as select * from dba_objects;

Table created.

--创建删除表的存储过程

create or replace procedure deleteTab

--插入语句

SQL> insert into test select * from dba_objects;

6374 rows created.

SQL> /

6374 rows created.

SQL> /

6374 rows created.

SQL> commit

--创建删除的存储过程

create or replace procedure deleteTab

/**

** Usage: run the script to create the proc deleteTab

**        in SQL*PLUS, type "exec deleteTab('Foo','ID>=1000000','3000');"

**        to delete the records in the table "Foo", commit per 3000 records.

**       Condition with default value '1=1' and default Commit batch is 10000.

**/

(

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!');

end;

/

--执行语句

SQL> exec deleteTab('TEST','object_id >0','10000')

你看看执行结果我试验过,效果还可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值