views:

2899

answers:

5

The title kind of says it all. 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

+5  A: 

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

Doug
A: 

Doug,

The real question is why you need the currval when you haven't used a nextval in your session? You can look at the column LAST_NUMBER of the USER/ALL/DBA_SEQUENCES view, but think of concurrency issues when you start to use that.

Regards, Rob.

Rob van Wijk
Since some developers were not using the sequence when inserting rows, the sequence had fallen out of sync. I was trying to get the current value so that I could determine the right increment for an ALTER SEQUENCE. Once I realized I needed a NEXTVAL first, I just used NEXTVAL instead, as it also suited my purposes.
Doug
+2  A: 

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!

Tony Andrews
A: 

use this

select sequence_name, to_char(min_value) min_value, to_char(max_value) max_value, increment_by, cycle_flag, order_flag, cache_size, to_char(Last_number) last_number from user_sequences where sequence_name='MYSEQUENCE'

HainKurt...

HainKurt
A: 

Thanks Kurt, it worked!

Potrocks