Oracle_DataArchive_Using_Procedure

Hi Expert,
I am working on a application  where all the records older than 6 months need to be move to history tables in the same schema, but on a different table space.

Looking for a stored procedure which can preform this process.

For ex. TABLE_A (live, latest 6 months) ==> TABLE_A_H (archive, older than 6 months, up to 8 years).

So far no is..

Thanks in Advance
MIHIR KAR#Hadoop #Oracle_DB #UNIX beginnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert GOracle dbaCommented:
you'll have to write it yourself
or if it's part of an erp package, see if it has an archiving feature

depending  on the size of the table, it might be best to move in pieces
and add a column for archiving

and you must have a column with a date in there

every change you make to table_a, you also need to make in the same order to table_a_h
(or provide the columns in the archiving)
alter table table_a add archived number(1) default 0 not null;
create table_a_h as select * from table_a where 1 = 0;

declare 
  delta number;
  moved number;
begin
  delta := 100000;
  moved := 0;
  loop 
    update table_a
    set archived = 1 
    where row_created < sysdate -180
      and rownum <= delta;
    moved := sql%rowcount;

    insert into table_a_h 
    select * from table_a where archived = 1;

    delete from table_a where archived = 1;
  
    commit;

    exit when moved < delta;
  end loop;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark GeerlingsDatabase AdministratorCommented:
I agree with the first line of Geert's comment: "you'll have to write it [or them] yourself".  

I really doubt that you want "all the records older than 6 months [to] be move[d] to history tables".  I suspect that your application has some master records (part numbers, customers, etc.) that you definitely do *NOT* want to move, no matter how old they are.

I believe that you have some transaction tables, and in those tables you may want to move all of the older records.  Do those tables all have a reliable date column (with a name something like "created_date") that you can use to determine how old the records are?

You will need to write a procedure for each of these tables.  I would suggest just using a query to identify the records to move, insert them them into the history table, then delete them (and make sure that the number of records inserted matches the number deleted) then commit the inserts and deletes, or do a rollback and figure out why the quantities don't match.)  

In theory, you may be able to use just one procedure for all of your tables that you want processed this way, that uses dynamic SQL to handle the different tables.  But, I don't recommend this approach.

The suggestion from Geert to add a column and update that column would make it easier to make sure that you get the same number of records copied to history and deleted.  But, adding a control column and doing the two updates (first setting all records to "0", then setting some to "1") may add a significant performance penalty.  And, this extra column may (or may not) cause problems for the application.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.