Skip to Main Content
  • Questions
  • Copy table data from one database to another with table partitions and references

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Nitin.

Asked: July 21, 2016 - 9:21 pm UTC

Last updated: July 25, 2016 - 8:51 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Guys,

Need your help.

I have 4-5 different tables in Oracle database(11g). I have to copy the data from all these tables from there(1st DB) to another database(2nd DB). Source database is having partitioned tables and data in one table could be linked/reference to another row in another table like foreign key.

I am trying to create a procedure, reason being : I need to schedule a job from Java program which will run at sometime and will execute this procedure which will copy the data from 1st database to 2nd database(with all partitions and references)

After the data is copied, I need to delete the data from 1st database so that the space could be recovered.

The data to be copied will be based on condition like copy all data older than 1 year for which I have written the SQL as

select * from schema1.table1 where last_updt_ts < add_months(SYSDATE, -12);

Basically, its a database archiving thing and I need to achieve it through mix of Java job scheduling and a stored procedure.

Source and Destination dB would be Oracle. But I also have to test this between Oracle(source) and MySql(destination).

Can anyone please give me some pointers?

Thanks
Nitin

and Chris said...

Why oh why do you have to call this from a Java program? And if the production use of this is Oracle -> Oracle, why are you testing Oracle -> MySQL?

Instead, create a database link between the two databases. The your can write a procedure that does something like:

create or replace procedure arch is
begin
  insert into t@arch_db
    select * from t 
    where  upd_dt < add_months(sysdate, -12);

  delete t
  where  upd_dt < add_months(sysdate, -12);
end arch;
/

You say the tables are partitioned. If this is on upd_dt then instead of the delete you can do a "drop partition". This will be faster and generate far less redo than the delete.

If you must use Oracle -> MySQL, look into using gateways for the link.

http://www.oracle.com/technetwork/middleware/id-mgmt/documentation/mysql-integration-guide-428960.pdf
https://blogs.oracle.com/db/entry/oracle_gateway_master_note

Once you've created this procedure, just use the scheduler so its runs once a month from your database. For example:

BEGIN
  DBMS_SCHEDULER.create_job (
    job_name        => 'arch_job',
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN arch; END;',
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=monthly;',
    enabled         => TRUE);
END;
/


https://oracle-base.com/articles/10g/scheduler-10g

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks

Nitin Kalra, July 22, 2016 - 5:27 pm UTC

Thanks Chris. I will be trying this and will update here.

-Nitin

Nitin Kalra, July 22, 2016 - 6:53 pm UTC

What about exception handling? If there comes an error or exception during the copy, how that gets reported?

Thanks
Nitin
Connor McDonald
July 25, 2016 - 8:51 am UTC

The same way any exceptions are? Oracle will raise one!

It's up to you to handle it appropriately. What exactly are you concerned about?

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library