8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OracleのSequenceの現在値をどうにかしたい

Posted at

例えば、今の値が

SQL> select sq_hoge.nextval from dual;

   NEXTVAL
 ----------
     12345

なんかだったりした場合、これを100001に変えたい時。
Oracleにはそんな気の利いた機能は存在しないので、泥臭く下記のようにやるのが一番手っ取り早い。

希望の値と現在の値の差を、そのまま増分値にして、

SQL> alter sequence sq_hoge increment by 87655;

ここで、nextvalを取得。

SQL> select sq_hoge.nextval from dual;

   NEXTVAL
 ----------
    100001

問題無ければ、大急ぎで増分値を元に戻す。

SQL> alter sequence sq_hoge increment by 1;

大急ぎでと言っても、実際には3行まとめてペーストしちゃうので、瞬時に処理は終わるけど。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?