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;
/
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.