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 7866877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:32:25+00:00 2026-06-03T00:32:25+00:00

I’m trying to create a script to migrate data from one DB to another.

  • 0

I’m trying to create a script to migrate data from one DB to another. One thing I’m not currently able to do is set the nextval of a sequence to the nextval of a sequence in another DB.

I got the difference in values from user_sequences and generated the following dynamic SQL statements:

execute immediate 'alter sequence myseq increment by 100';
execute immediate 'select myseq.nextval from dual';
execute immediate 'alter sequence myseq increment by 1';

commit;

But nothing happens. What am I missing? If I run the same statements outside the procedure, they work fine:

alter sequence myseq increment by 100;
select myseq.nextval from dual;
alter sequence myseq increment by 1;

commit;

EDIT: Apologies to all for not being clear. I’m actually altering the sequence in the same DB. I’m only getting the value to be set from a remote DB. Perhaps it was unnecessary to mention the remote DB as it doesn’t affect things. I only mentioned it to explain what my goals were.

Step 1. I get the nextval of the sequence from a remote DB.

select (select last_number
        from dba_sequences@remoteDB
        where upper(sequence_name) = upper(v_sequence_name)) - (select last_number
                                                                from user_sequences
                                                                where upper(sequence_name) = upper(v_sequence_name)) increment_by
from dual;    

Step 2. I generate dynamic SQL statements with this value:

execute immediate 'alter sequence myseq increment by 100';
execute immediate 'select myseq.nextval from dual';
execute immediate 'alter sequence myseq increment by 1';

commit;

No error was raised, but nothing happened. When I wrote the SQL statements with DBMS_OUTPUT.PUT_LINE and ran them outside they worked.

  • 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-06-03T00:32:26+00:00Added an answer on June 3, 2026 at 12:32 am

    Here is some code which dynamically sets a sequence to a new (higher) value. I have written this so it will work for any sequence in your schema.

    create or replace procedure resync_seq
        (p_seq_name in user_sequences.sequence_name%type)
    is
        local_val pls_integer;
        remote_val pls_integer;
        diff pls_integer;
    begin
        execute immediate 'select '|| p_seq_name ||'.nextval from dual'
               into local_val;
        select last_number into remote_val
        from user_sequences@remote_db
        where sequence_name = p_seq_name ;
        diff := remote_val - local_val;
    
        if diff > 0
        then
            execute immediate 'alter sequence  '|| p_seq_name ||' increment by ' ||to_char(diff);
            execute immediate 'select '|| p_seq_name ||'.nextval from dual'
               into local_val;
            execute immediate 'alter sequence  '|| p_seq_name ||' increment by 1';
        end if;
    
    end;
    

    The procedure doesn’t need a COMMIT because DDL statements issue an implicit commit (two in fact).

    You can execute it and see the synced value like this (in SQL*PLus):

    exec resync_seq('MYSEQ')
    select myseq.currval
    from dual
    

    Incidentally, the only way to reset a sequence (to its original starting value or a different lower value) is dropping and re-creating the sequence.


    In 18c Oracle added a RESTART capability to ALTER SEQUENCE. The straightforward option …

    alter sequence myseq restart;
    

    …resets the sequence to the value specified by the START WITH clause in the original CREATE SEQUENCE statement. The other option allows us to specify a new starting point:

    alter sequence myseq restart start with 23000;
    

    Excitingly this new starting point can be ahead or behind the current value (within the usual bounds of a sequence).

    The one snag is that this new capability is undocumented (only for Oracle’s internal usage) and so we’re not supposed use it. Still true in 20c. The only approved mechanism for changing a sequence’s value is what I outlined above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.