currval()

Synopsis

Use the currval( sequence_name ) function to return the last value returned by the nextval( sequence_name ) function for the specified sequence in the current session.

Semantics

_sequencename

Specify the name of the sequence.

  • An error is raised if nextval( sequence_name ) has not been called for the specified sequence in the current session.

Examples

Create a sequence

  1. yugabyte=# CREATE SEQUENCE s;
  1. CREATE SEQUENCE

Call nextval().

  1. yugabyte=# SELECT nextval('s');

  1. nextval

  1. 1

(1 row)

  1. yugabyte=# SELECT currval('s');

  1. currval

  1. 1

(1 row)

Call currval() before nextval() is called.

  1. yugabyte=# CREATE SEQUENCE s2;
  1. CREATE SEQUENCE
  1. SELECT currval('s2');
  1. ERROR: currval of sequence "s2" is not yet defined in this session

See also