ORA-08002: sequence MYSEQUENCE.CURRVAL is not yet defined in this session

ORA-08002: sequence MYSEQUENCE.CURRVAL is not yet defined in this session error occurs when you try to call the CURRVAL of a sequence before calling the NEXTVAL of the sequence in the session. After you’ve created a sequence, you should call NEXTVAL. There will be no value assigned to the CURRVAL of the newly created sequence. The NEXTVAL increments the sequence value and assigns in the CURRVAL. Otherwise, the error ORA-08002: sequence MYSEQUENCE.CURRVAL is not yet defined in this session will occur.

The newly created sequence has no value at this time. In this session, the sequence has yet to be defined. The sequence value will be incremented and the current value of the sequence will exist if the next value is invoked in the sequence. The sequence’s NEXTVAL should be called in the newly created sequence before calling the sequence’s CURRVAL to get the current value.



The Problem

The current value is not allocated to the newly created sequence. The error “ORA-08002: sequence MYSEQUENCE.CURRVAL is not yet defined in this session” will be thrown if the current value is used in the sequence. To get the current value, the sequence should be given a value. In the sequence, the next value command finds the next possible value and assigns it to the current value. The CURRVAL command returns the current value of the sequence after invoking the NEXTVAL command in the sequence.

create sequence mysequence start with 1 increment by 1 nocycle;

select mysequence.currval from dual;

Error

ORA-08002: sequence MYSEQUENCE.CURRVAL is not yet defined in this session
08002. 00000 -  "sequence %s.CURRVAL is not yet defined in this session"
*Cause:    sequence CURRVAL has been selected before sequence NEXTVAL
*Action:   select NEXTVAL from the sequence before selecting CURRVAL


Solution

The sequence’s NEXTVAL should be used in the newly created sequence. The NEXTVAL command finds the next feasible value in the sequence and assigns it as the sequence’s current value. The current value of the sequence will be returned in the database if the CURRVAL command is used after that. The error “ORA-08002: sequence MYSEQUENCE.CURRVAL is not yet defined in this session” will be resolved if NEXTVAL is called before calling the CURRVAL in the sequence.

create sequence mysequence start with 1 increment by 1 nocycle;

select mysequence.nextval from dual;

select mysequence.currval from dual;

Output

select mysequence.nextval from dual; --> returns 1

select mysequence.currval from dual; --> returns 1