大数据量删除与插入的PLSQL语句

原创|其它|编辑:郝浩|2009-02-27 01:59:35.000|阅读 1934 次

概述:大数据量删除与插入的PLSQL语句

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

在PL/SQL中的命令窗口执行下面的语句:(先将下面代码粘贴到命令窗口,粘贴完后,再在窗口里输入"/"键则运行你刚粘贴到窗口里的该程序代码)

【1】业务逻辑:A表中有300W的数据,B表中有70W的数据,现在要对A表中的部分数据进行删除,删除条件是:根据B表中70W数据来对A表进行条件删除,条件的字段为usercode。

 

set serveroutput on
    
Declare
       rcode 
integer;
    
begin
          loop
            
--每次将表a删除10000条记录
            delete from a
             
where exists (select usercode from b where a.usercode = b.usercode)
               
and rownum < 10000;
            
--每10000条提交一次
            commit;
            
exit when sql%rowcount = 0;
          
end loop;
          rcode :
= 1;
          
--delete操作完成后,rcode值为1
          dbms_output.put_line('结果是:'||to_char(rcode));
    exception
      
when others then
        
rollback;
        rcode :
= 0;
        
--delete操作失败后,rcode值为0
        dbms_output.put_line('结果是:'||to_char(rcode));
    
end;

语句的逻辑:每循环一次,删除a表中的10000条记录,并commit提交该delete语句,当整个循环执行完后,70W数据成功删除完后,会在命令窗口打印出(delete出现异常则打印0):

结果是:1
PL/SQL 过程成功完成

【2】业务逻辑:批量插入5000调测试数据到susr_info表和susr_basic表中,做为测试数据。

    set serveroutput on
    
Declare
      cnt             
number(10);
      v_userindex     
number(20);
      v_customerindex 
number(20);
      v_usercode      
varchar(100);
      rcode           
varchar(200);
      v_message       
varchar(200);
    
begin
          cnt :
= 1;
          v_usercode:
=16500000000;
          loop
                
begin
                      sp_get_next_seq(
'susr_info',v_customerindex);--序列器产生v_customerindex
                      sp_get_next_seq('susr_basic',v_userindex);--同上
                      v_usercode := v_usercode + 1;
                      
insert into susr_info
                      (customerindex,
                       customerid,
                       usertruename,
                       useremail,
                       userpwd
                       )
                       
values
                      (v_customerindex,
                       v_customerindex,
                       
'' || v_usercode,
                       
' ',
                       
'55555'
                       );
                       
insert into susr_basic
                      (userindex,
                       usercode,
                       accountindex,
                       customerindex,
                       msisdntype
                       )
                       
values
                      (v_userindex,
                       
'' || v_usercode,
                       
0,
                       v_customerindex,
                       
1
                       );
                      
if mod(v_userindex, 1000= 0 then
                        
commit;
                      
end if;
                exception
                
when others then
                  
rollback;
                  v_message :
= sqlcode||':'||sqlerrm;
                  
--将异常记录做日志
                  insert into log_info values('zxdbm_ismp.v3_sub_2',v_message,sysdate,v_usercode); 
                  
commit;
                
end
                cnt :
= cnt + 1;
                
if cnt > 5000 then
                  
exit;
                
end if;   
          
end loop;
          
commit;
          rcode :
= 'over';
          dbms_output.put_line(
'结果是:'||rcode);
    
end;



语句的逻辑:每循环1000次,将LOOP循环体中的两个insert语句的结果commit,每次循环,如果insert语句只其中一个出现异常,则将异常记录写到日志表log_info中,接着执行下一次循环,当执行insert的次数达到5000,则退出循环,结束整个业务流程。执行完后,会在命令窗口打印出

结果是:over
PL/SQL 过程成功完成


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP