Using nextval function to get sequence next value : nextval « Sequence « PostgreSQL






Using nextval function to get sequence next value

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=#

           
       








Related examples in the same category