In my procedure I write this
INSERT INTO questions(id, value) VALUES(my_seq.NEXTVAL, p_question);
INSERT INTO DEPENDENCIES(parent_question_id, child_question_id)
VALUES (my_seq.CURRVAL, my_seq.NEXTVAL);
Let's say sequence's last value equals to 1. In this case I expect this result:
my_seq.NEXTVAL = 2;
my_seq.CURRVAL = 2;
my_seq.NEXTVAL = 3;
But it inserts this:
my_seq.NEXTVAL = 2;
my_seq.CURRVAL = 3;
my_seq.NEXTVAL = 3;
I would like to know why does oracle retrieve sequence value in this way?
CURRVAL. returns the current value of a sequence. NEXTVAL. increments the sequence and returns the next value.
CURRVAL : Returns the current value of a sequence. NEXTVAL : Increments the sequence and returns the next value.
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.
If you had three separate statements :
nextval
currval
nextval
Then you would be correct. But you only have two
nextval
currval and nextval
Currval and nextval are part of a single atomic step - currval does not get processed before nextval.
You'll need to use variables for this :
DECLARE
l_parent_question_id NUMBER ;
l_child_question_id NUMBER ;
BEGIN
l_parent_question_id := my_seq.NEXTVAL ;
INSERT INTO questions(id, value) VALUES(l_parent_question_id, p_question);
l_child_question_id := my_seq.NEXTVAL ;
INSERT INTO DEPENDENCIES(parent_question_id, child_question_id)
VALUES (l_parent_question_id, l_child_question_id);
END ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With