Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 221235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:00:51+00:00 2026-05-11T19:00:51+00:00

As in the question, how do I automatically reset an Oracle sequence’s value back

  • 0

As in the question, how do I automatically reset an Oracle sequence’s value back to 0 every year in Oracle 10g?

I’m using the sequence to generate an identifier in the format YYYY<sequence value> and the sequence value has to be reset to 0 every year.

YYYY is obtained from java and concatenated with the sequence value from Oracle. The format of the identifier can’t be changed due to external 3rd party requirements. Thanks for any help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:00:52+00:00Added an answer on May 11, 2026 at 7:00 pm

    Sequences aren’t really designed to be reset. But there are some cases where resetting a sequence is desirable, for example, when setting up test data, or merging production data back into a test environment. This type of activity is not normally done in production.

    IF this type of operation is going to be put into production, it needs to thoroughly tested. (What causes the most concern is the potential for the reset procedure to be accidentally performed at the wrong time, like, in the middle of the year.

    Dropping and recreating the sequence is one approach. As an operation, it’s fairly straightforward as far as the SEQUENCE goes:

        DROP SEQUENCE MY_SEQ;
        CREATE SEQUENCE MY_SEQ START WITH 1 INCREMENT BY 1 MINVALUE 0;
    

    [EDIT] As Matthew Watson correctly points out, every DDL statement (such as a DROP, CREATE, ALTER) will cause an implicit commit. [/EDIT]

    But, any privileges granted on the SEQUENCE will be dropped, so those will need to be re-granted. Any objects that reference the sequence will be invalidated. To get this more generalized, you would need to save privileges (before dropping the sequence) and then re-grant them.

    A second approach is to ALTER an existing SEQUENCE, without dropping and recreating it. Resetting the sequence can be accomplished by changing the INCREMENT value to a negative value (the difference between the current value and 0), and then do exactly one .NEXTVAL to set the current value to 0, and then change the INCREMENT back to 1. I’ve used a this same approach before (manually, in a test environment), to set a sequence to a larger value as well.

    Of course, for this to work correctly, you need to insure no other sessions reference the sequence while this operation is being performed. An extra .NEXTVAL at the wrong instant will screw up the reset. (NOTE: achieving that on the database side is going to be difficult, if the application is connecting as the owner of the sequence, rather than as a separate user.)

    To have it happen every year, you’d need to schedule a job. The sequence reset will have to be coordinated with the reset of the YYYY portion of your identifier.

    Here’s an example:

    http://www.jaredstill.com/content/reset-sequence.html

    [EDIT]

    UNTESTED placeholder for one possible design of a PL/SQL block to reset sequence

        declare
          pragma autonomous_transaction;
          ln_increment       number;
          ln_curr_val        number;
          ln_reset_increment number;
          ln_reset_val       number;
        begin
    
          -- save the current INCREMENT value for the sequence
          select increment_by
            into ln_increment
            from user_sequences
           where sequence_name = 'MY_SEQ';
    
          -- determine the increment value required to reset the sequence
          -- from the next fetched value to 0
          select -1 - MY_SEQ.nextval into ln_reset_increment from dual;
    
          -- fetch the next value (to make it the current value)
          select MY_SEQ.nextval into ln_curr from dual;
    
          -- change the increment value of the sequence to 
          EXECUTE IMMEDIATE 'alter sequence MY_SEQ increment by '
            || ln_reset_increment ||' minvalue 0';
    
          -- advance the sequence to set it to 0
          select MY_SEQ.nextval into ln_reset_val from dual;
    
          -- set increment back to the previous(ly saved) value
          EXECUTE IMMEDIATE 'alter sequence MY_SEQ increment by '
            || ln_increment ;
        end;
        /
    

    NOTES:

    • how to best protect the sequence from access while it’s being reset, RENAME it?
    • Several test cases to work through here.
    • First pass, check normative cases of positive, ascending, increment 1 sequence.
    • would a better approach be to create new SEQUENCE, add permissions, rename existing and new sequences, and then re-compile dependencies?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 144k
  • Answers 144k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Reusability I see two approaches. You can either create individual… May 12, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer Code metrics Do you find cyclomatic complexity a useful measure?… May 12, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer Basically, nvarchar means you can handle lots of alphabets, not… May 12, 2026 at 8:34 am

Related Questions

As in the question, how do I automatically reset an Oracle sequence's value back
Here is my current situation: I have a web page containing a couple scrollable
this may come as pretty odd but I'm now working on a membership based
I have a UITextView that has a lot of content. I have a button
Encoding issues are among the one topic that have bitten me most often during

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.