Application of nextVal and currVal in the database., nextvalcurrval

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.