PostgreSQL/Sequence/nextval

Материал из SQL эксперт
Версия от 13:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Using nextval function to get sequence next value

   <source lang="sql">

postgres=# postgres=# postgres=# CREATE SEQUENCE myseq MINVALUE 0; CREATE SEQUENCE postgres=# postgres=# -- Using currval(?) postgres=# postgres=# SELECT currval("myseq"); ERROR: currval of sequence "myseq" is not yet defined in this session postgres=# postgres=# postgres=# drop sequence myseq; DROP SEQUENCE postgres=# postgres=# -- Setting a sequence value postgres=# CREATE SEQUENCE myseq MINVALUE 0; CREATE SEQUENCE postgres=# postgres=# SELECT setval("myseq", 1010);

setval

  1010

(1 row) postgres=# postgres=# SELECT nextval("myseq");

nextval

   1011

(1 row) postgres=# postgres=# postgres=# SELECT setval("myseq", 1010, false);

setval

  1010

(1 row) postgres=# postgres=# SELECT nextval("myseq");

nextval

   1010

(1 row) postgres=# postgres=# drop sequence myseq; DROP SEQUENCE postgres=#

      </source>