不需要存储过程
update aa set col=(select tt.id from (select rownum id,t.* from (select * from aa order by col) t) tt where tt.col=aa.col);

解决方案 »

  1.   

    thanks,
    to  bzszp(SongZip) ,
    是新增数据,不是修改数据.
      

  2.   

    新增的时候,在目前的aa表中字段col的值中选取一个未用的
    select col+1 from aa a where not exists ( select 1 from aa b where b.col=(a.col+1));
      

  3.   

    若是要最小的,可以
    select min(new_value) from
    (  select col+1 new_value from aa a where not exists ( select 1 from aa b where b.col=(a.col+1)) );若是任一个不连续的值,可以
    select col+1 new_value from aa a where not exists ( select 1 from aa b where b.col=(a.col+1)) and rownum < 2;后者的效率要高很多。
      

  4.   

    多谢各位,
    to  daydayupliq(强强),
    以前是用序列,用触发器应该怎么做?
      

  5.   

    create trigger a_tri
    before insert or update or delete on a
    for each row
    declare
    v_id varchar2(20);
    begin
    select min(id) into v_id from a where (lead(id,1,0) over(order by rownum)-id)>1;
    if inserting then
    select v_id into :new.id from dual;
    end if;
    end;
    /