Application of nextVal and currVal in the database., nextvalcurrval
X. nextVal gets the next sequence value of x sequence, and x. currVal gets the current sequence value of x sequence.
Note:X.CurrVal must be referenced beforeX. NextVal; otherwise, a ORA-08002 is reported: The sequence x. currVal is not defined in this session.
Specific Application
1. Create a sequence:
Create sequence test_seq increment by 1 start with 1 maxvalue 30 minvalue 0 nocycle cache 10 order;
Select sequence test_seq.nextVal from dual; // each call nextVal, the sequence value increases once (the incremental value is customized)
Select sequence test_seq.currVal from dual;
2. Create a table:
Create table test (col1 int, col2 int );
3. Insert record:
Insert into test values (0, 0 );
4. Use nextVal
Insert into test (col1, col2) values (test_seq.nextval, test_seq.nextval); // you can guess whether the values of col1 and col2 are the same?
If there are two records with col1 values of 2 in the test table, then:
Update test set col2 = test_seq.nextval where col1 = 2; // you can guess whether the value of col2 is the same after update?