I will explain NEXTVAL and CURRVAL function in Oracle in this post.
NEXTVAL and CURRVAL function in Oracle
You need the unique sequential values in the database for the SQL and PL/SQL Codes. The sequence is created for this purpose in the database.
Oracle Sequence object has 2 important function like NEXTVAL and CURRVAL.
NEXTVAL – Next Value of Sequence
This function is used to increment the sequence and returns the next value of related sequence.
CURRVAL – Current Value of Sequence
This function is used to return the current value of the sequence.
NEXTVAL and CURRVAL examples are as follows.
SQL> create sequence seq_msd; Sequence created. SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 1 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 2 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 3 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 4 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 5 SQL> select seq_msd.currval from dual; CURRVAL ---------- 5 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 6 SQL> select seq_msd.currval from dual; CURRVAL ---------- 6 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 7 SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 8 SQL> select seq_msd.currval from dual; CURRVAL ---------- 8 SQL>
CURRVAL function must be called after NEXTVAL funtion. Otherwise you will get the following error.
SQL> select seq_msd.currval from dual; select seq_msd.currval from dual * ERROR at line 1: ORA-08002: sequence SEQ_MSD.CURRVAL is not yet defined in this session SQL> select seq_msd.nextval from dual; NEXTVAL ---------- 9 SQL> select seq_msd.currval from dual; CURRVAL ---------- 9 SQL>
Do you want to learn Oracle Database Performance Tuning detailed, then Click this link.
Performance Tuning and SQL Tuning Tutorial in the Oracle Database