Reset oracle sequence

Hello world

with the help of an ETL tool I'm migrating data in oracle database tables here I do not use oracle sequence. After the migration, I have to reset the sequence.

ex: table: employee, name of the sequence: employee_id_seq

initially:

create sequences employee_id_seq

start by increment of 1 by 1;

data in the Employee table:

employee_id ename

--------               ---------

1                    aa

5                    bb

6                    cc

to restore order, I drop this sequence and recreate the sequence

IE.,.

employee_id_seq sequence of fall;

Max (employee_id) here is 6 nextvalue is 7.

create sequences employee_id_seq

starts with 7 increment of 1;

Select double employee_id_seq.nextval;

nextval:

--------

7

instead of delete and re-create the sequence any other method is there to reset the sequence.so, I want to create a dynamic script that will reset all of the sequences.

Please advise on this.

Thank you

Jery

Hi all

script below works fine

CREATE OR REPLACE PROCEDURE SP_DYNAMIC_RESET_SEQ_SCRIPT

as

SQLSTR1 varchar (40);

SQLSTR2 varchar (40);

SQLSTR3 varchar (40);

SQLSTR4 varchar (40);

number of max_val_incol;

number of max_val_currval;

T1 varchar (1000);

Q2 varchar (1000);

T3 varchar (1000);

Start

for c in (select TABLE_NAME from M_ALL_DYNAMIC_DRIVER_san)

loop

SQLSTR1: = c.table_name; -This will store the name of the table [emp, dept]

SQLSTR2: = upper (TRIM (SQLSTR1)) | ' _ID'.

SQLSTR3: = upper (TRIM (SQLSTR1)) | ' _ID_SEQ';

SQLSTR4: = SQLSTR3 |'. NEXTVAL';

RUN IMMEDIATELY "SELECT MAX ('|)" SQLSTR2 |') OF ' | SQLSTR1 IN max_val_incol;

Q1: = 'ALTER SEQUENCE |  SQLSTR3 | ' INCREMENT ' | max_val_incol;

EXECUTE IMMEDIATE q1;

eXECUTE IMMEDIATE 'SELECT'. SQLSTR4 | 'FROM DUAL"IN max_val_currval;

Q2: = 'ALTER SEQUENCE |  SQLSTR3 | ' INCREMENT OF 1';

EXECUTE IMMEDIATE T2;

end loop;

END;

Thank you all, helped on that.

Tags: Database

Similar Questions

  • Duplicate values generated by an Oracle sequence

    Hello

    Since a few days we are facing a weird problem in our application where the sequences seem to be generating duplicate values. Previously I thought is this is not possible, but now it seems to be a problem. Here are the details:

    Select * from v$ version;

    Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production

    PL/SQL Release 11.2.0.2.0 - Production

    CORE Production 11.2.0.2.0

    AMT for Linux: Version 11.2.0.2.0 - Production

    NLSRTL Version 11.2.0.2.0 - Production

    We use to insert it into a table column of the last sequence value that is of course the primary key of the table column. Today, we received a violation of unique constraint on the column of the primary key (which is driven by the sequence). Below is the last value of the PK column

    SELECT MAX(PCCURVEDETAILID) OF T_PC_CURVE_DETAILS;

    162636

    and below, was the last value in the sequence that has been less than the PK column

    SELECT S_PCCURVEDETAILS. NEXTVAL FROM DUAL;

    162631

    Then we checked the data of the user_sequences and found the next sequence value (LAST_NUMBER) to be 162645 which was greater than the current value of the sequence:

    Select * de user_sequences one a.sequence_name as "S_PCCURVEDETAILS" ;

    SEQUENCE_NAME

    MIN_VALUE

    MAX_VALUE

    INCREMENT_BY

    CYCLE_FLAG

    ORDER_FLAG

    CACHE_SIZE

    LAST_NUMBER

    S_PCCURVEDETAILS

    1

    1E + 27

    1

    N

    N

    20

    162645

    I then modified the nocache sequence:

    change sequence S_PCCURVEDETAILS NoCache ;

    Select * de user_sequences one a.sequence_name as "S_PCCURVEDETAILS" ;

    SEQUENCE_NAME

    MIN_VALUE

    MAX_VALUE

    INCREMENT_BY

    CYCLE_FLAG

    ORDER_FLAG

    CACHE_SIZE

    LAST_NUMBER

    S_PCCURVEDETAILS

    1

    1E + 27

    1

    N

    N

    0

    162633

    SELECT S_PCCURVEDETAILS. NEXTVAL FROM DUAL;

    162632

    We also face the same type of problem of violation of primary key to another sequence-driven table, a few days before.

    In our code, I also checked that the primary key in the table value is inserted using only the value of the sequence.

    Can anyone please suggest any reason this issue for

    Concerning

    Deepak

    I faced a similar problem about 3 years ago. Here's the story, suddenly in my UAT environment a job that inserts data in some tables began to throw exceptions of primary key violation. After investigation, I found that the sequence is x, but the primary key for the table is x + y. investigation more - DBA moved all production data without even notifying the application support team.

    Morale - pretty obvious

  • ORDER/ALL in oracle sequences

    Hello
    I'm a single instance database running on Oracle 10.2.0.4.
    If I have a sequence created using the ORDER clause, oracle will ensure that sequence numbers are still classified?
    I understand that the sequences will have gaps. It is correct. I just want to know that it will be always ordered.

    Is it really important if we use the COMMAND/ALL for unique instances? What happens if we give ALL?

    I have read the oracle documentation, and I still have these questions.

    Thank you
    Delphine

    user13312943 wrote:
    Hello
    I'm a single instance database running on Oracle 10.2.0.4.
    If I have a sequence created using the ORDER clause, oracle will ensure that sequence numbers are still classified?
    I understand that the sequences will have gaps. It is correct. I just want to know that it will be always ordered.

    Is it really important if we use the COMMAND/ALL for unique instances? What happens if we give ALL?

    I have read the oracle documentation, and I still have these questions.

    Thank you
    Delphine

    Seems pretty clear in the documentation I looked upwards
    http://docs.Oracle.com/CD/B19306_01/server.102/b14200/statements_6015.htm#SQLRF01314
    "
    ORDER specify in order to make sure that sequence numbers are generated in order of demand. This clause is useful if you use numbers in sequence as the timestamps. Guarantor of order is generally not important for sequences used to generate primary keys.

    * ORDER is necessary only to guarantee ordered generation if you are using Oracle with Real Application Clusters database. If you are using exclusive mode, the sequence numbers are always generated in order.*

    ALL specify ALL if you do not want to guarantee sequence numbers are generated in order of demand. It is the default value.
    "

    See you soon,.

  • Oracle sequence number

    Hello

    My oracle server is 11g in environment.and BECAUSE I have this is the sequence.



    CREATE SEQUENCES "BUS_FILE_SEQ_NB."
    MINVALUE MAXVALUE 1 999999999999999999999999999 INCREMENT OF 1 START WITH 36134
    TO CACHE ALL NOCYCLE 20;

    SELECT BUS_FILE_SEQ_NB. NEXTVAL FROM DUAL;

    Due to this CAR, my sequence number is random, it is not in order as 1,2,3... (But its unique)


    But I need to select records recent query using max (seq_column).
    Someone you will suggest that how do to change the sequence to get arrested.


    Thank you.

    Vanessa wrote:
    Hello

    My oracle server is 11g in environment.and BECAUSE I have this is the sequence.

    CREATE SEQUENCES "BUS_FILE_SEQ_NB."
    MINVALUE MAXVALUE 1 999999999999999999999999999 INCREMENT OF 1 START WITH 36134
    TO CACHE ALL NOCYCLE 20;

    SELECT BUS_FILE_SEQ_NB. NEXTVAL FROM DUAL;

    Due to this CAR, my sequence number is random, it is not in order as 1,2,3... (But its unique)

    I don't believe you.
    SEQUENCE values always increase; but the gaps that may exist.

    post SQL & results that show the value of the sequence is less than the previous.

    >
    >

    Vanessa wrote: but I need to select records recent query using max (seq_column).
    Someone you will suggest that how do to change the sequence to get arrested.

    Thank you.

    Hello

    My oracle server is 11g in environment.and BECAUSE I have this is the sequence.

    CREATE SEQUENCES "BUS_FILE_SEQ_NB."
    MINVALUE MAXVALUE 1 999999999999999999999999999 INCREMENT OF 1 START WITH 36134
    TO CACHE ALL NOCYCLE 20;

    SELECT BUS_FILE_SEQ_NB. NEXTVAL FROM DUAL;

    Due to this CAR, my sequence number is random, it is not in order as 1,2,3... (But its unique)

    But I need to select records recent query using max (seq_column).
    Someone you will suggest that how do to change the sequence to get arrested.

    Thank you.

  • Oracle sequence issue

    CREATE SEQUENCE F_SQ
      START WITH 1
      MAXVALUE 999999999999999999999999999
      MINVALUE 1
      NOCYCLE
      CACHE 20
      NOORDER;
    
    

    I have a sequence is created using the above script. Our night jobs using these sequences, as well on the first day that he used 1,2,3,4,5 and the next day has begun with 21,22,23... and so on

    I understand that's the setting of the CACHE.


    Here are the questions

    For three days, it summer jump between the two figures.

    The sequence will never decide to use these skipped numbers? (by above example 6 to 20), if etc. of circumstance and is it possible to avoid it?

    But our PROD servers are running in exclusive mode, which means the sequence will keep producing in ascending order No?... I don't bother about the shortcomings.

    Yes there is no reason why it is not ordered if you are single-instance. But if you want to be sure, just put the ORDER in the sequence. There is no overload when you are single-instance.

    Kind regards

    Franck.

  • Reset the sequence in the workflow or the mapping

    I tried to simply by creating the procedure and insertion in the workflow. but I get the following error:

    PRE-01003: A condition of infrastructure has prevented the request to complete. PRE-01038: unable to evaluate the expression declare expression VARCHAR2 (4000): = SEQ_TRADE; Start: result: = owbsys.wb_rt_conversions.from_char (expression); end;. Please change the expression, redeploy and start over again. ORA-06550: line 1, column 42:
    PLS-00357: Table, view or reference sequence "SEQ_TRADE" not allowed in this context
    ORA-06550: line 1, column 24:
    PL/SQL: Ignored Element
    ORA-06550: line 1, column 107:
    PLS-00320: the declaration of the type of the expression is incomplete or incorrect
    ORA-06550: line 1, column 61:
    PL/SQL: Statement ignored

    the procedure is simple and it works fine when running Toad:

    CREATE OR REPLACE PROCEDURE DWH. "RESET_SEQS"(seq_name IN VARCHAR2).
    IS
    BEGIN
    RUN IMMEDIATELY 'DROP SEQUENCE DWH.' | seq_name;
    IMMEDIATE EXECUTION "CREATE SEQUENCE DWH." | seq_name |
    "START WITH 1
    MAXVALUE 999999999999999999999999999
    MINVALUE 1
    NOCYCLE
    NOCACHE
    ALL ';
    END;

    Any suggestions? TX

    Try to join seq_name input parameter value of the processing activity in processflow with a single quotation mark (i.e. change SEQ_TRADER 'SEQ_TRADER')

    Kind regards
    Oleg

  • Question about Oracle sequences

    Please let me know if this question is not the subject of this forum and advise which forum I should post it. Thank you.

    We have a database 10g running on the AIX servers. A process of data loading was recently, and the development team is of the opinion that the question can be corrupted in the database sequence. This sequence is used to generate a unique primary key for writing in a table.

    Are there problems with 10g about the objects sequence database becomes corrupted? If so, is there a Metalink note published on a fix?

    Thank you all for a light on this topic.

    Hello

    Interview the max value of your column of tabale, in order to know the populeted of the maximum value of the sequence, compare it your currval in the sequence, and the maximum value for the column. If the values are not the same and the currval is lower than the max value, you will probably have the constraint violation, then increment the value of your sequence curr.

    Concerning

  • Sequence increment without a fall

    Oracle 10.2.0.

    I need increment a sequence after that I have to perform an import of certain data.

    I tried increment the sequence but it takes much too long if the numbers are very large.
    (in the order of 7 digits). We have potentially start at 1, but have no idea of the numbers we are brought into the import.

    Is there anyway I can change the sequence without falling and recreate, as it makes my body of package not valid.

    Refer to this.
    http://oracleintellect.blogspot.com/2009/11/reset-Oracle-sequence-value.html

    Kind regards
    Vijay

  • ORA-04092 immediate execution may engage in a trigger - Reset sequence

    Hi all. I'm trying to reset a sequence of a procedure that is triggered in a trigger. The reason is that after each new transaction, a new series of point number must be generated, numbering each line as 1, 2, 3... After a new transaction, I need to start from the beginning again. The procedure is
    PROCEDURE Reset_Item_Serial_No IS
      
        Serial_No  NUMBER;
        p_seq_name VARCHAR2(20);
      
      BEGIN
      
        p_seq_name := 'SERIAL_NO_SEQ';
      
        EXECUTE IMMEDIATE 'SELECT ' || p_seq_name || '.nextval FROM dual'
          INTO Serial_No;
      
        EXECUTE IMMEDIATE 'ALTER sequence ' || p_seq_name || ' INCREMENT BY -' ||
    
                          Serial_No || ' MINVALUE 0';
      
        EXECUTE IMMEDIATE 'SELECT ' || p_seq_name || '.nextval FROM dual'
          INTO Serial_No;
      
        EXECUTE IMMEDIATE 'ALTER sequence ' || p_seq_name ||
                          ' INCREMENT BY 1 MINVALUE 0';
      
      END;
    This procedure is triggered by the following section in the trigger:
     SELECT COUNT(*)
          INTO v_Reset_Sequence
          FROM supply_items s
         WHERE s.requisition_code = v_refno;
      
        IF v_Reset_Sequence = 0 THEN
        
          account_interface.reset_item_serial_no;
        
        END IF;
    I can not use TRANSACTION INDEPENDENT of PRAGMA, the db detects this and restores. Advice, is to make the enforcement procedure, or somehow create a sequence number, whenever I need to insert a new transaction?

    In your case, I'd rather not a sequence. As your requirment is transaction after I'd go with a GTB (global temporary Table).

    Here is an example.

    SQL*Plus: Release 10.2.0.1.0 - Production on Fri Jan 27 14:58:12 2012
    
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP and Data Mining options
    
    SQL> drop table test
      2  /
    
    Table dropped.
    
    SQL> create table test (no integer, name varchar2(10))
      2  /
    
    Table created.
    
    SQL> drop table test_seq_storage
      2  /
    
    Table dropped.
    
    SQL> create global temporary table test_seq_storage (seq integer) on commit delete rows
      2  /
    
    Table created.
    
    SQL> create or replace trigger test_bi_statement before insert on test
      2  declare
      3    lcount integer;
      4  begin
      5    select count(*) into lcount from test_seq_storage;
      6    if lcount = 0 then
      7      insert into test_seq_storage (seq) values(1);
      8    end if;
      9  end;
     10  /
    
    Trigger created.
    
    SQL> create or replace trigger test_bi_row before insert on test for each row
      2  declare
      3    lseq integer;
      4  begin
      5    select seq into lseq from test_seq_storage;
      6    :new.no := lseq;
      7    update test_seq_storage
      8       set seq = lseq+1;
      9  end;
     10  /
    
    Trigger created.
    
    SQL> show error
    No errors.
    SQL> select * from test
      2  /
    
    no rows selected
    
    SQL> insert into test (name)
      2  select 'karthick'
      3    from dual
      4  connect by level <= 5
      5  /
    
    5 rows created.
    
    SQL> select * from test
      2  /
    
            NO NAME
    ---------- ----------
             1 karthick
             2 karthick
             3 karthick
             4 karthick
             5 karthick
    
    SQL> insert into test (name)
      2  select 'Oracle'
      3    from dual
      4  connect by level <= 5
      5  /
    
    5 rows created.
    
    SQL> select * from test
      2  /
    
            NO NAME
    ---------- ----------
             1 karthick
             2 karthick
             3 karthick
             4 karthick
             5 karthick
             6 Oracle
             7 Oracle
             8 Oracle
             9 Oracle
            10 Oracle
    
    10 rows selected.
    
    SQL> commit
      2  /
    
    Commit complete.
    
    SQL> insert into test (name)
      2  select 'SQL'
      3    from dual
      4  connect by level <= 5
      5  /
    
    5 rows created.
    
    SQL> select * from test
      2  /
    
            NO NAME
    ---------- ----------
             1 karthick
             2 karthick
             3 karthick
             4 karthick
             5 karthick
             6 Oracle
             7 Oracle
             8 Oracle
             9 Oracle
            10 Oracle
             1 SQL
             2 SQL
             3 SQL
             4 SQL
             5 SQL
    
    15 rows selected.
    
    SQL> commit
      2  /
    
  • Reset sequence

    Hello

    I created a sequence:

    CREATE SEQUENCES ORDER_SEQ.

    order of creation.

    After that I used it a few times...

    How can reset this sequence for 0 again (I can't change the sequence creation script, because it is limited)?

    Thank you
    Vinod

    Correct way would be to drop the sequence and re-create it.

    To restore order, you can use script below:

    declare
      v_val   number;
    begin
      select seq.nextval into v_val from dual;
      dbms_output.put_line('Value :: ' || v_val);
      execute immediate 'alter sequence seq increment by ' || ( 1 - v_val);
      select seq.nextval into v_val from dual;
      dbms_output.put_line('New Value :: ' || v_val);
      execute immediate 'alter sequence seq increment by 1';
    end;
    
  • How to create a sequence in oracle forms6i

    Oracle forms 6i


    Hai All

    I work in a leave request entry, so I need to create a sequence to give a unique number for each entry

    Pls tell me the steps how to create and how to call the sequence of database


    Thanks in advance

    Srikkanth.m

    Hello
    Sequence, you can create in the database and you can use this sequence generation sequence number forms
    Creation, see link below...
    http://www.techonthenet.com/Oracle/sequences.php
    http://psoug.org/reference/sequences.html

    To generate numbers forms use like that...

    For each entry at the time of save you can use on PRE-INSERT of block level
    SELECT SEQUENCE_NAME.NEXTVAL
    INTO :FORM_FIELD_NAME
    FROM DUAL;
    

    -Clément

  • TDATAMAP - sequence of Datakey

    Hello

    Migration of FDM to FDMEE, I combined the two tables of datamaps into one and reset the datakey sequentially.

    Now, however, when I add a new card, I get a "too many objects match the primary key ADF_FACES-60096" error, probably because it points to an Oracle sequence I need to reset.

    Can someone tell me what the Oracle sequence used for datakeys parameter cards to?

    order of the cards is TDATAMAP_DATAKEY_S

  • Rights DBA on the sequence

    Hi all

    I have a problem regarding sequences, for example:

    -patterns 2 (P1, P2)
    -1 seqence (P1. SEQ_100)

    Diagram of the P2 has no rights DBA. To add the grant I use GRANT SELECT ON P1. SEQ_100 P2

    But when I let it drop and then re-create sequence P1. SEQ_100 normally the subsidy disappears as well. Is there a way which grant stays or is a few different grant that is available (active) on P2 schema, even if we lower and that create the sequence.

    Response would be greatly appreciated.

    Best regards, Robert

    Well, I expected that you want to reset the sequence, is it not? If you want to reset the sequence, then there is another way! Take a look at this:
    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:1119633817597

  • change the sequence currentvalue

    How can I change my sequence so 1'000 ' 000 value of this attribute 'NextVal' would return me now? It is reset to the current value of the sequence. It is possible.
    I see no such possibility here:
    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/statements_2011.htm

    I tried successfully a this sentence:
    ALTER SEQUENCE Casino.Networkplayers_Code_Seq minvalue 1000000000--ORA-04007: MINVALUE cannot be made to exeed the current value
    Or should I do arithmetic as is done here:
    http://www.lifeaftercoffee.com/2005/10/20/changing-the-current-value-of-Oracle-sequences/

    Published by: CharlesRoos on 29.10.2010 16:46

    Hello

    Here's a way, if you can guarantee that no one else uses the sequence for one minute. (Revoke privileges if necessary)

    Find the current value.
    Add 1 to that and subtract your desired value.
    Proceed to the this amount increment sequence.
    A new issue.
    Change back to the original amount INCREMENT.
    The next issue genereate after this will be the value you want.

    For example:

    REVOKE SELECT ON seq_x FROM PUBLIC;
    
    SELECT  seq_x.NEXTVAL
    FROM    dual;
    
    NEXTVAL
    -------
        178
    

    1 000 000 - (178 + 1) = 999821, continue like this:

    ALTER SEQUENCE seq_x  INCREMENT BY 999821;
    
    SELECT  seq_x.NEXTVAL
    FROM    dual;
    
    NEXTVAL
    -------
     999999
    
    ALTER SEQUENCE seq_x  INCREMENT BY 1;
    
    GRANT SELECT ON seq_x TO PUBLIC;
    

    Published by: Frank Kulash, October 29, 2010 09:52
    Added example

  • Running script SQL on oracle forms10g

    Hi expert,

    How I can run a script inside the done oracle 10 g sql. ? I want to create a button that would trigger this script. This script would reset the sequence in our DB server.

    Thank you

    so, put this script in a folder on your appicationserver and use host to call sqlplus with this script

    HOST('sqlplus user/pwd@db @thefolder\thescript.sql');
    

Maybe you are looking for