当我想往一个表(table1)插入记录时,首先判断这个表有没有相同的记录(select count(*) into var1 from table1 where ...),如果记录少的时候这种方法是可行的,但碰到很多记录的时候就非常的慢,如十万条记录差不多要一个小时,大家又没更好的方法呢?

解决方案 »

  1.   

    1、如果你不想对已有记录进行累加操作,可以这样:
    begin
        insert into .......;
        exception when dup_val_on_index then null(或其他操作);
    end;dup_val_on_index 判断数据是否有主键重复2、如果你想对已有记录进行累加操作,可以这样:
    begin
        update tablename set .........;
        if  SQL%NOTFOUND  then
           insert into .........;
        end if;
    end;SQL%NOTFOUND 判断update是否失败
      

  2.   

    改成(select /*+first_rows*/count(*) into var1 from table1 where ...)试试
      

  3.   

    9i里有一个这样的函数,大家又没用过:
    merge into(param...)using(select...)when is match then(insert..)
    大家又没用过?可是我用的是8i啊!