Setting a new sequence value for an existing sequence

This tips helps you to import the sequence value from the source to the target schema.

This script is useful when you have to import the sequence value from the source to the target schema, keeping

the CURRVAL the same in the target schema. It will replace the old value of the sequence with the given new sequence value provided through a flat file consisting of sequence name and new sequence value:

-- Pass a flat file consisting of Sequence Name and New Values as a parameter to this sql file.

-- set_new_seq_values.sql

define SEQ_NAME=&1
define NEW_VAL=&2

set pages 0 
set ver off 
set wrap off 
set feed off 
set term off

col old_inc_val noprint new_value old_inc_val
col cur_inc_val noprint new_value cur_inc_val

select INCREMENT_BY old_inc_val from user_sequences
 where sequence_name='&SEQ_NAME';

select ( &NEW_VAL - &SEQ_NAME..NEXTVAL ) cur_inc_val from dual;

alter sequence &SEQ_NAME
 INCREMENT BY &&cur_inc_val;

select &SEQ_NAME..NEXTVAL from dual;

alter sequence &SEQ_NAME
  INCREMENT BY &&old_inc_val;

set term on

prompt

select 'Sequence &SEQ_NAME has been set to &NEW_VAL..' from dual;

prompt

EXIT

For More Information


This was first published in May 2001

Dig deeper on Oracle and SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close