Oracle reset sequence (reconstruction mode not deleted)

Source: Internet
Author: User

In Oracle, when auto-incrementing sequence is reset to initial 1, it is deleted and rebuilt. This method has many drawbacks, functions and stored procedures dependent on it will become invalid and need to be re-compiled. However, there is a clever way to do this without deleting it. You can use the step size parameter to first find the nextval of sequence, remember to change the increment value to the negative value (which goes in turn), and then change it back. Assume that the sequence name to be modified is seq_name 1, select seq_name.nextval from dual; // assume that the result is 5656 2. alter sequence seq_name increment by-5655; // note that it is-(n-1) 3. select seq_name.nextval from dual; // run the following command again to reset it to 1. 4. alter sequence seq_name increment by 1. // you can write a stored procedure to restore it, the following is a complete stored PROCEDURE, and then you can call the passing parameter: create or replace procedure seq_reset (v_seqname VARCHAR2) AS n NUMBER (10); tsql VARCHAR2 (100 ); begin execute immediate 'select' | v_seqname | '. nextval from dual 'into n; n: =-(n-1); tsql: = 'alter sequence '| v_seqname | 'crement by' | n; execute immediate tsql; execute immediate 'select' | v_seqname | '. nextval from dual 'into n; tsql: = 'alter sequence '| v_seqname | 'crement by 1'; execute immediate tsql; END seq_reset;

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.