Hi Expert,

I have live transaction table called t1,t2,t3,t4 which contains data from last 2 years.

my requrement is i want to delete the data which is older than 14 days

and want to backup the deleted data into bkp_t1, bkp_t2, bkp_t3, bkp_t4.

please guide me how can i perform these action in a plsql procedure .

thanks in advance .
MIHIR KAR#Hadoop #Oracle_DB #UNIX beginnerAsked:
Who is Participating?
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.

slightwv (䄆 Netminder) Commented:
I would probably do it as two separate statements:

insert into bkp_t1(col1, col2, col3) select col1,col2,col3 from t1 where some_date_column < trunc(sysdate)-14;
delete from t1 where some_date_column < trunc(sysdate)-14;

Do the same for the other 3 tables.

Another option is a delete trigger on t1 that inserts into the bkp table.  Then all you need is the delete statement.

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
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
Thanks Slightwv,
Please verify the below procedure once and suggest if i can do changes for better works .

I have created  below proc for inserting and deleting data, i'm not sure this process is correct or not but it working well as of now for one table

create or replace procedure p_bkp_tbl
-- truncating records from bkp_t1  
execute immediate 'truncate table bkp_t1; '
--inserting old data from live table to bkp_t1 table
insert into bkp_t1
select * from t1 where trunc(crt_date) <= trunc(sysdate) - 14;
--deleting records except last 14 days
delete from t1 where trunc(crt_date) <= trunc(sysdate) - 14;
slightwv (䄆 Netminder) Commented:
I prefer using actual column names like I posted in my example instead of 'select *' and the generic insert.  The problem with not using column names is if one of the tables changes, the generic insert will stop working.

Since you are truncating the table first, you can also add some error checking with a select count(*) into bkup_t1_count from bkup_t1.  Then use SQL%ROWCOUNT after the delete to compare the two statements.  If the counts aren't the same, then you can take action like rollback.

Other than that, if you think it works, then it looks good to me.
Geert GOracle dbaCommented:
why truncate ?
if you by accident execute the procedure twice, you don't have anything

i'd write a separate delete statement for backup data older than 2 years

there is no point in trunc'ing your date column
it's actually wrong
you'll delete 15 days -2 seconds if you run this a second past midnight

just insert what isn't in the backup table yet
merge into bckup d
using (select pk, col1, col2, col3 from t1 where crt_date <= trunc(sysdate)-14)  s on ( =
when not matched then 
  insert (pk, col1, col2, col3) values(, s.col1, s.col2, s.col3);

Open in new window

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.