Oracle Reset sequence (rebuild not removed) ____oracle

Source: Internet
Author: User

Oracle typically resets the sequence to initial 1 o'clock, which is deleted and rebuilt, which has many drawbacks, and the functions and stored procedures that depend on it will fail and need to be recompiled.

But there is a clever way, do not delete, using the step parameters, first detect the sequence of the Nextval, remember, the increment to negative this value (the reverse), and then change back.

Assume the name of the sequence you want to modify: Seq_name

1, select Seq_name.nextval from dual; Assuming that the results are 5656

2, alter sequence seq_name increment by-5655; Note IS-(n-1)

3. Select Seq_name.nextval from dual;//check again, walk, reset to 1

4. Alter sequence seq_name increment by 1;//restore

You can write a stored procedure, and the following is a complete stored procedure, and then invoke the argument:

Create or Replace procedure Seq_reset (V_seqname varchar2) as n number (a);
TSQL VARCHAR2 (m);
 Begin
 Execute immediate ' SELECT ' | | v_seqname| | '. Nextval from dual ' into N;
  n:=-(n-1);
  tsql:= ' alter sequence ' | | v_seqname| | ' increment by ' | | n;
  Execute immediate TSQL;
 Execute immediate ' SELECT ' | | v_seqname| | '. Nextval from dual ' into N;
  tsql:= ' alter sequence ' | | v_seqname| | ' increment 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.