存储过程部分如下:
 
 --定义一个游标
 cursor mytemp is
       select id,mobilephone from mytable where  trim(lanmuid) =mylanmuid and kaiguan =1;       
 --开始遍历游标得到其值    
  for  sms in  mytemp loop
      
      insert into send_box(content,usernumber) 
   values(content,sms.mobilephone) ; 
  end Loop;
我的问题是send_box这个表中对字段content,usernumber作了约束unique key不能重复,且这个表有很多的程序来向其中插入数据,虽然在上面这个循环中本身的数据不会重复,但是不能保证不会和此表中已有的数据产生重复,就会产生约束unique key异常。所以我想在此循环中获取异常,如果异常发生就跳过,然后继续循环下面的数据,不能让此循环中止,因为中止后没法找到那个断点,接着循环了。先谢谢了。

解决方案 »

  1.   

    for  sms in  mytemp loop
          begin
          insert into send_box(content,usernumber) 
       values(content,sms.mobilephone) ; 
          exception
               when dup_val_on_index then
               null;
          end;
      end Loop;
    --看看这样行么
      

  2.   

    上面的兄弟:我试过了 出错 如下:PLS-00103: 出现符号 "EXCEPTION"在需要下列之一时:
     begin case declare
       end exit for goto if loop mod null pragma raise return select
       update while with <an identifier>
       <a double-quoted delimited-identifier>
      

  3.   

    begin
    for  sms in (select * from b_areas) loop
          begin
       if (sms.area_id='M') then
          dbms_output.put_line(sms.area_id);
       else
           raise dup_val_on_index ;
       end if;
          exception
               when dup_val_on_index  then
               dbms_output.put_line('null');
       null;
          end;
      end Loop;
      end;
    --
    null
    null
    null
    null
    null
    null
    M
    null
      

  4.   

    begin
    for  sms in (select * from b_areas) loop
          begin
       --if (sms.area_id='M') then
          dbms_output.put_line(sms.area_id);
       --else
       --    raise dup_val_on_index ;
       --end if;
          --exception
           --    when dup_val_on_index  then
           --    dbms_output.put_line('null');
    --   null;
          end;
      end Loop;
      end;
    --
    N
    H
    X
    O
    E
    S
    M
    W
      

  5.   

    问题还没有解决
    我这样试:
    for  sms in  mytemp loop
          begin
          insert into send_box(content,usernumber) 
       values(content,sms.mobilephone) ; 
          exception
               when dup_val_on_index then
               null;
          end;
      end Loop;
    确实出现上面的错误。
      

  6.   

    在插入之前用content,usernumber 作检索吧 对机能影响应该不大
      

  7.   

    exception
     when dup_val_on_index  then
    ...
      

  8.   

    for  sms in  mytemp loop
          begin
         insert into send_box(content,usernumber) 
       values(content,sms.mobilephone) ; 
    end Loop;
    exception
       when dup_val_on_index then
       null;
    end;
    这样再试试
      

  9.   

    for  sms in  mytemp loop
          begin
         insert into send_box(content,usernumber) 
       values(content,sms.mobilephone) ; 
    end Loop;
    exception
       when others then
       null;
    end;
    这肯定没错了……
      

  10.   

    在函数中增加一个判断唯一性得函数:
     cursor mytemp is
          select id,mobilephone from mytable where  trim(lanmuid) =mylanmuid and kaiguan =1;      
     --开始遍历游标得到其值   
      for  sms in  mytemp loop
         --判断content,usernumber是否在 send_box中是否存在
         if is_exists_send_box(content,usernumber) = 1 then
           --若存在退出这次循环
           exit;
         end if;
                
         insert into send_box(content,usernumber) 
       values(content,sms.mobilephone) ; 
      end Loop;
      

  11.   

    icedut(冰) ( ) 信誉:100    Blog 
    方法确实真的很好啊!再多试试!