NEXTVAL and CURRVAL function in Oracle

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

 

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

Leave a Reply

Your email address will not be published. Required fields are marked *