ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

oracle 分批插入 分批删除

2021-09-12 17:01:32  阅读:336  来源: 互联网

标签:cur 分批 varchar2 插入 yz rowid oracle tb delete


create or replace procedure insbigtab
(   p_TableName       in    varchar2,
  p_Condition       in    varchar2,
  p_Count        in    number,
  p_insettab in    varchar2
) 
is
 Type v_rowid is table of varchar2(100) index by binary_integer;
 type type_cursor  is ref cursor; 
 v_cur type_cursor ;
 var_rowid v_rowid;
 sql1 varchar2(1000);

 tb varchar2(1000);
 co varchar2(1000);
 pc number;
 pinst varchar2(1000);
begin 
 tb:=p_TableName;
 co:=p_Condition;
 pc:=p_Count;
 pinst:=p_insettab;
 sql1:='select  ROWID from '||tb||' where  '||co;
  open v_cur for sql1 ;
  loop
   fetch v_cur bulk collect
     into var_rowid limit pc;
    forall i in 1 .. var_rowid.count
     execute  immediate 'insert into '||pinst||' select * from '||tb||' where ROWID = :rn '
   USING  var_rowid(i);
     commit;
     dbms_lock.sleep(3);
   exit when v_cur%NOTFOUND or v_cur%NOTFOUND is null;
   END LOOP ;
  close v_cur;
end;

exec yz.insbigtab('yz.t1','object_id>5000',10000,'yz.t2');

create or replace procedure delbigtab
(
p_TableName in varchar2,
p_Condition in varchar2,
p_Count in varchar2
)
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;
dbms_lock.sleep(3);
end loop;
commit;
DBMS_OUTPUT.PUT_LINE('Finished!');
DBMS_OUTPUT.PUT_LINE('Totally '||to_char(n_delete)||' records deleted!');
end;

 exec yz.delbigtab('yz.t1','object_id>5000','10000');

delbigtab

标签:cur,分批,varchar2,插入,yz,rowid,oracle,tb,delete
来源: https://www.cnblogs.com/omsql/p/15259154.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有