Reset sequence to a specific value

前端 未结 1 1057
独厮守ぢ
独厮守ぢ 2021-01-21 18:06

We\'re creating a \'blank\'/minimal copy of an existing database and want to reset one of the sequences to a value. Putting the number in the below works, but I want to make it

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 18:32

    You can use a negative increment to reset a sequence to a lower value - this script (it's just a PL/SQL block version of yours) will work with sequence values larger than 9999 without problems):

    declare
     currval pls_integer;
     diff pls_integer;
    begin
      select SQ_USER_ID.nextval into currval from dual;
      dbms_output.put_line('value before alter: ' || currval);
      diff := 99999 - currval;
      dbms_output.put_line('diff: ' || diff);
      execute immediate ' alter sequence SQ_USER_ID INCREMENT BY ' ||  diff || 'nocache';
      select SQ_USER_ID.nextval into currval from dual;
      dbms_output.put_line('value after alter: ' || currval);
      execute immediate 'alter sequence SQ_USER_ID INCREMENT BY 1  cache 20';
    end;
    

    0 讨论(0)
提交回复
热议问题