Using a Sequence

A sequence generates a series of numbers. A sequence contains two pseudo columns named currval and nextval. currval has the current value from the sequence. nextval has the next value from the sequence.

Before retrieving the current value, you must initialize the sequence by retrieving the next value. When you select my_seq.nextval the sequence is initialized to 1.


CREATE SEQUENCE my_seq;

SQL> SELECT my_seq.nextval
  2  FROM dual;

   NEXTVAL
----------
         1

SQL>

Once the sequence is initialized, you can get the current value from the sequence by retrieving currval. For example:


SQL> SELECT my_seq.currval
  2  FROM dual;

   CURRVAL
----------
         1

SQL>

When you retrieve currval, nextval remains unchanged. nextval only changes when you retrieve nextval to get the next value.


SQL> SELECT my_seq.nextval, my_seq.currval FROM dual;

   NEXTVAL    CURRVAL
---------- ----------
         2          2

SQL>
Home »
Oracle »
Table » 

Sequence:
  1. Creating a Sequence
  2. Retrieving Information on Sequences
  3. Using a Sequence
  4. Populating a Primary Key Using a Sequence
  5. Modifying a Sequence
  6. Using Sequences in PL/SQL
  7. Dropping a Sequence
Related: