DB utilities – updating sequences the elegant way

0 0
Read Time:38 Second

Ever needed to update a sequence so the next value is greater than the maximum value of a table? Well, the quick and dirty ways are

  1. select SEQ.nextval from dual;

    (ever did this a million times?)

  2. drop sequence SEQ;
    create sequence SEQ start with MAX+1;
    

    (never had problems with grants and invalided stored procedures?)

If you’re interested in a more elegant and robust way…

The SQL*Plus script upd_seq.sql allows you to specify a sequence, table and column. The script updates the sequence so that the next value is the smallest value larger than the current maximum of the table column (unless the current sequence value is already at least the maximum table column value).

See the documentation in the script for more information.

About Post Author

Gert-Jan Paulissen

Senior Oracle architect / developer (PL/SQL & Apex) / DevOps engineer.I am happy to be able to combine work and hobby. Software engineer since 1989. Bridge player since 1977 and dutch champion more than 10 times.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

2 thoughts on “DB utilities – updating sequences the elegant way

  1. Hello Gert-Jan

    A nice dynamic solution but what about the following:

    — select the max value from the table
    Select the max_value into lMaxVal from table;
    –select the sequence number from the sequence
    Select sequence_name.nextval into lSeqNr from dual;
    — change the increment of the sequence
    Execute immediate (‘alter sequence sequence_name increment by ‘||( lMaxVal-lSeqNr));
    — select a value from the sequence
    select sequence_name.next_val into lSeqNr from dual;
    — reset the increment of the sequence
    Execute immediate (‘alter sequence sequence_name increment by 1’);

    If you implement this in your package you do not have to recreate/grant etc on the sequence and the sequence would not create
    invalid objects.

    Cheers Marcos

Comments are closed.

Next Post

How to implement an unique key constraint in ADF BC?

In an ADF project that deals with building a membership administration application for a sporting organization, I ran against the question how I could implement the validation of the uniqueness of an alternative key. For example, an entity organization member has a primary key member_id. However it would be nice […]
%d bloggers like this: