Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving CURRVAL and NEXTVAL in a single query

Using SQLPlus, I have the following sequence generated:

CREATE SEQUENCE pubnum_seq
START WITH 7
    INCREMENT BY 2
MAXVALUE 1000;

What I want to do is write a single query that retrieves both the NEXTVAL and the CURRVAL in the sequence. It seems like it should be simple enough. I've tried the following statement:

SELECT pubnum_seq.currval, pubnum_seq.nextval
FROM dual;

However this retrieves the same value for both.

CURRVAL - 7
NEXTVAL - 7

If I try the statements separately, then it gives me what I want, but I'm stump as to how to do this in one query.

like image 965
tworley1977 Avatar asked Dec 04 '25 15:12

tworley1977


1 Answers

Well, after searching through the docs at Oracle, I'm guessing that my statement is correct after all. According to this docs page:

If a statement contains references to both CURRVAL and NEXTVAL, Oracle increments the sequence and returns the same value for both CURRVAL and NEXTVAL regardless of their order within the statement.

Doesn't quite make sense to me for Oracle to do that, but I'll take their word for it.

like image 84
JasonMArcher Avatar answered Dec 07 '25 05:12

JasonMArcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!