Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2003
    Posts
    1

    Unanswered: creating sequence that starts from 1 every new year

    I need to create a sequence that will be part of of a unique key. The sequence should start from 1 and increase by 1. I need the sequence to start from 1 for every new year. E g I want to have keys 2003-<product_id>-<component_id>001 ...2003--<product_id>-<component_id>676. Then, year 2004 I create a new record and that record should have the key 2004--<product_id>-<component_id>001. How can I do that in a simple way? Do I have to write pl/sql code that drops and creates the sequence every January the 1st?

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1

    Re: creating sequence that starts from 1 every new year

    Rather than drop and re-create the sequence, which will invalidate stored procedures etc, you can reset the sequence to 1 as follows:

    declare
    s number;
    begin
    select seq.nextval into s from dual;
    execute immediate 'alter sequence seq increment by -'||s||' minvalue 0';
    select seq.nextval into s from dual;
    execute immediate 'alter sequence seq increment by 1';
    end;
    /

    But I don't understand why the key you are using should consist of year, product_id, component_id and a sequence number - when year and sequence number alone will be unique.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •