7

My question seems easy but i haven't found a solution. I'm using the nexval sequence in a insert SQL script but we have errors because the LAST_NUMBER value haven't been updated and its value is not correct with tabla data. For this case for example the value is 5 and the table has 62 rows.

I've searched on google but don't found the command or the way yo change the LAST_NUMBER parameter of the sequence.

ALTER SEQUENCE rs2qnes0 START WITH 62;

or ALTER SEQUENCE rs2qnes0 LAST_NUMBER 62; don't work

2 Answers 2

12

Change increment by, select next value, set increment back to original value:

SQL> create sequence s1 start with 5;

Sequence created.

SQL> select last_number from user_sequences where sequence_name = 'S1';

LAST_NUMBER
-----------
          5

SQL> alter sequence s1 increment by 57;

Sequence altered.

SQL> select s1.nextval from dual;

   NEXTVAL
----------
        61

SQL> alter sequence s1 increment by 1;

Sequence altered.

SQL> select last_number from user_sequences where sequence_name = 'S1';

LAST_NUMBER
-----------
         62

Or use RESTART:

SQL> alter sequence s1 restart start with 77;

Sequence altered.

SQL> select last_number from user_sequences where sequence_name = 'S1';

LAST_NUMBER
-----------
         77
1
  • Doesn't work for system-generated sequences (identity columns).
    – Chris
    Commented Feb 9, 2023 at 17:00
-1

This post is for changing Last Number :)

Let us assume LAST_NUMBER in seq xyz is 3953 and you want to change it to 205233, then you need to increment the sequence by (205233 - 3953 = 201,280) and then increment it by 1. that can be achieved by below statements:

to change the LAST_NUMBER to

ALTER SEQUENCE xyz INCREMENT BY 201280;
SELECT xyz.nextval from dual;
ALTER SEQUENCE xyz INCREMENT BY 1;

Now the LAST_NUMBER would be updated as 205233

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.