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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:22:54+00:00 2026-05-23T08:22:54+00:00

Is it possible to copy the current values of sequences in a schema to

  • 0

Is it possible to copy the current values of sequences in a schema to another database? The sequences have already been created in both databases. This is in Oracle.

Edit:

Based on the help below, once the database link is set up, this script will make sure that the target database sequence values are greater than or equal to the source database values. The motivation for this is so that we don’t get primary key errors after copying data, so the fact that the target numbers are not exact is no problem.

set serveroutput on
DECLARE
  CURSOR GetCursorsToSync
  is
  SELECT a.sequence_name, a.last_number last_number_a, b.last_number last_number_b
  FROM user_sequences@SOURCE_DB a, user_sequences b
  where a.sequence_name = b.sequence_name
  and a.last_number != b.last_number;

  type CursorsTableType is table of GetCursorsToSync%rowtype index by pls_integer;  
  CursorsTable  CursorsTableType;
  i             pls_integer;

  PROCEDURE reset_sequence(
      sequence_name IN VARCHAR2,
      source_value  IN NUMBER,
      target_value  IN NUMBER )
  IS
    l_sql varchar2(4000);
    l_temp number(30);
  BEGIN
    IF source_value <= target_value THEN
      RETURN;
    END IF;

    dbms_output.put_line(sequence_name || ' ' || source_value || ' ' || target_value);
    l_sql := 'alter sequence '|| sequence_name || ' increment by '||to_char(source_value-target_value);
    dbms_output.put_line(l_sql);
    EXECUTE immediate l_sql;
    l_sql := 'SELECT '|| sequence_name || '.nextval FROM dual';
    dbms_output.put_line(l_sql);
    EXECUTE immediate l_sql into l_temp;
    dbms_output.put_line(l_temp);
    l_sql := 'alter sequence '|| sequence_name || ' increment by 1';
    dbms_output.put_line(l_sql);
    EXECUTE immediate l_sql;
    COMMIT;
  END reset_sequence;
BEGIN
  open GetCursorsToSync;
  fetch GetCursorsToSync bulk collect into CursorsTable;
  close GetCursorsToSync;
  commit;

  i := CursorsTable.first;
  while i is not null loop
    reset_sequence(CursorsTable(i).sequence_name,
      CursorsTable(i).last_number_a,CursorsTable(i).last_number_b);
    i := CursorsTable.next(i);
  end loop;
end;
/
  • 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-05-23T08:22:55+00:00Added an answer on May 23, 2026 at 8:22 am

    A combination of UltraCommits statements and a database link, in addition to a stored procedure that you can schedule to automatically run, would serve you well.

    --drop create db_link
    DROP DATABASE LINK SOURCE_DB;
    
    CREATE DATABASE LINK "SOURCE_DB"
      CONNECT TO USER IDENTIFIED BY password USING 'SOURCE_DB';
    
     --drop create sequences 
      DROP sequence target_seq;
    CREATE sequence target_seq start with 6;
    
      --the next two lines run in source db
      DROP sequence source_seq;
    CREATE sequence source_seq start with 6000;
    
    --take a look at the sequences to get an idea of what to expect
    SELECT source_schema.source_seq.nextval@SOURCE_DB source_seq,
      target_seq.nextval target_seq
    FROM dual; 
    
    --create procedure to reset target sequence that you can schedule to automatically run
    CREATE OR REPLACE
    PROCEDURE reset_sequence
    AS
      l_source_sequence pls_integer;
      l_target_sequence pls_integer;
      l_sql VARCHAR2(100);
    BEGIN
      SELECT source_schema.source_seq.nextval@SOURCE_DB,
        target_seq.nextval
      INTO l_source_sequence,
        l_target_sequence
      FROM dual;
      l_sql := 'alter sequence target_seq increment by '||to_number(l_source_sequence-l_target_sequence);
      EXECUTE immediate l_sql;
      SELECT target_seq.nextval INTO l_target_sequence FROM dual;
      l_sql := 'alter sequence target_seq increment by 1';
      EXECUTE immediate l_sql;
      COMMIT;
    END reset_sequence;
    /
    
    --execute procedure to test it out
    EXECUTE reset_sequence;
    
    --review results; should be the same
    SELECT source_schema.source_seq.nextval@SOURCE_DB, target_seq.nextval FROM dual;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is it possible to copy a variable like this this? class Colours { var
Is it possible to copy the value of a group into another one within
Possible Duplicate: Where do I find the current {X} standard? I have a simple
I still very new using Subversion. Is it possible to have a working copy
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
Is it possible to copy a single file to multiple directories using the cp
Is it possible to copy a 32 bit value into an array of 8
Is it possible to copy a table (with definition, constraints, identity) to a new
I would like to know if it is possible to copy/move files to a
I would like to ask if it is possible to copy/move all the objects

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.