Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: sequence MySequence.currval is not yet defined in this session

What does this mean, and how can I get around it?

SELECT MySequence.CURRVAL FROM DUAL; 

Result:

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

like image 328
Doug Avatar asked Apr 30 '09 21:04

Doug


People also ask

What is Currval in Oracle?

CURRVAL. returns the current value of a sequence. NEXTVAL. increments the sequence and returns the next value. You must qualify CURRVAL and NEXTVAL with the name of the sequence: sequence.CURRVAL sequence.NEXTVAL.

What value Currval holds Once you create a sequence?

The correct answer is c) Returns the last value across all nodes.

What is Oracle sequence Nextval?

The Oracle NEXTVAL function is used to retrieve the next value in a sequence. The Oracle NEXTVAL function must be called before calling the CURRVAL function, or an error will be thrown. No current value exists for the sequence until the Oracle NEXVAL function has been called at least once.


2 Answers

mysequence.CURRVAL returns the latest value that was obtained from sequence mysequence in your session, and hence isn't defined until you have obtained a value using mysequence.NEXTVAL at least once in the session. The purpose of CURRVAL is to let you use the sequence value more than once in your code e.g.

insert into parent (parent_id, ...) values (mysequence.NEXTVAL, ...);  insert into child (parent_id, ...) values (mysequence.CURRVAL, ...); 

If CURRVAL just returned the last value obtained from the sequence by any session, then it would be useless in the above code, and in fact could lead to data corruption!

like image 63
Tony Andrews Avatar answered Sep 22 '22 04:09

Tony Andrews


It turns out that you can't use CURRVAL until you have used NEXTVAL at least once in your session.

like image 33
Doug Avatar answered Sep 22 '22 04:09

Doug