如果你真的要这样做,可以考虑在里面用个嵌套一个程序块,
blnreture:=false;
v_a:=...;
loop
...
begin
    strInsert:='insert into t values('||v_a||',4,7);
    execute immediate strInsert;
exception when  dup_val_on_index then 
    v_a:=v_a+1;  
end
...
end loop

解决方案 »

  1.   

    to  wenyuan(苑) 
    能这样写?把exception写到循环中?而且我觉得这样也不能解决我的问题啊,用什么作为循环条件呢?能不能在异常中捕获异常? 比如:
    exception when dup_val_on_index then
      ...
      execute immediate strInsert;
      exception when others then
        ...我这样写出错了,该如何写?
      

  2.   

    declare temp int;
    begin
      execute immediate 'select count(0) from t where col1='||v_a into temp;
      while temp>0 loop
        v_a:=v_a+1;
        execute immediate 'select count(0) from t where col1='||v_a into temp;
      end loop;  strInsert:='insert into t values('||v_a||',4,7)';
      execute immediate strInsert;
    end;按照这个意思应该能达到你的要求
      

  3.   

    to ORARichard(没钱的日子......) 
    我问这个问题的关键就是不想在插入前先查询,这样我认为效率很低。因为表中可能有几十万条数据,而主键重复的可能性又很小,如果为了那很少的一部分数据而去对每一个插入数据进行比较,这样太不值得了。
      

  4.   

    declare
       strInsert  varchar2(100):='insert into t values('||v_a||',4,7);
    begin
       begin
          execute immediate strInsert;
       exception when dup_val_on_index then  --违反唯一键约束
          v_a:=v_a+1;  
          execute immediate strInsert;
       end;
    end;
      

  5.   

    to zzwind5()
     exception when dup_val_on_index then  该异常可以被多次触发吗?也就是当v_a加了1后若还重复,我希望让它再加1,直到可以插入为止。