How do I reset a sequence in Oracle?

后端 未结 18 1254
天命终不由人
天命终不由人 2020-11-22 05:09

In PostgreSQL, I can do something like this:

ALTER SEQUENCE serial RESTART WITH 0;

Is there an Oracle equivalent?

18条回答
  •  囚心锁ツ
    2020-11-22 05:42

    In my project, once it happened that someone manually entered the records without using sequence, hence I have to reset sequence value manually, for which I wrote below sql code snippet:

    declare
    max_db_value number(10,0);
    cur_seq_value number(10,0);
    counter number(10,0);
    difference number(10,0);
    dummy_number number(10);
    
    begin
    
    -- enter table name here
    select max(id) into max_db_value from persons;
    -- enter sequence name here
    select last_number into cur_seq_value from user_sequences where  sequence_name = 'SEQ_PERSONS';
    
    difference  := max_db_value - cur_seq_value;
    
     for counter in 1..difference
     loop
        -- change sequence name here as well
        select SEQ_PERSONS.nextval into dummy_number from dual;
     end loop;
    end;
    

    Please note, the above code will work if the sequence is lagging.

提交回复
热议问题