Oracle

更改序列中的 LAST_NUMBER 值 (NEXTVAL)

  • February 8, 2022

我的問題似乎很簡單,但我還沒有找到解決方案。我在插入 SQL 腳本中使用了 nexval 序列,但我們有錯誤,因為 LAST_NUMBER 值尚未更新,並且它的值與 tabla 數據不正確。例如,對於這種情況,值為 5,表有 62 行。

我在Google上搜尋過,但沒有找到命令或更改序列的 LAST_NUMBER 參數的方式。

ALTER SEQUENCE rs2qnes0 START WITH 62;

或者ALTER SEQUENCE rs2qnes0 LAST_NUMBER 62;不工作

更改增量,選擇下一個值,將增量設置回原始值:

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

或使用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

引用自:https://dba.stackexchange.com/questions/239966