Skip to content

Instantly share code, notes, and snippets.

@jzi96
Last active August 29, 2015 14:18
Show Gist options
  • Save jzi96/01810032ce5f3b87c285 to your computer and use it in GitHub Desktop.
Save jzi96/01810032ce5f3b87c285 to your computer and use it in GitHub Desktop.
Oracle delete data from large tables keeping only a few data (temp table)
--DROP TABLE acctmp3;
DECLARE
table_name varchar(20);
begin
table_name := 'acctmp';
DBMS_OUTPUT.PUT_LINE('Deleting table ' ||table_name);
execute immediate 'truncate table ' || table_name;
execute immediate 'drop table ' || table_name;
exception
when OTHERS then
DBMS_OUTPUT.PUT_LINE('Failed to delete table ' || table_name);
end;
DECLARE
table_name varchar(20);
begin
table_name := 'acctmp2';
DBMS_OUTPUT.PUT_LINE('Deleting table ' ||table_name);
execute immediate 'truncate table ' || table_name;
execute immediate 'drop table ' || table_name;
exception
when OTHERS then
DBMS_OUTPUT.PUT_LINE('Failed to delete table ' || table_name);
end;
DECLARE
table_name varchar(20);
begin
table_name := 'acctmp3';
DBMS_OUTPUT.PUT_LINE('Deleting table ' ||table_name);
execute immediate 'truncate table ' || table_name;
execute immediate 'drop table ' || table_name;
exception
when OTHERS then
DBMS_OUTPUT.PUT_LINE('Failed to delete table ' || table_name);
end;
BEGIN TRANSACTION;
DECLARE
keepDays number;
BEGIN
keepDays := 30;
dbms_output.put_line ('tso_fahrplan_fluss handling') ;
execute immediate 'LOCK TABLE tso_fahrplan_fluss IN EXCLUSIVE MODE';
execute immediate 'CREATE GLOBAL TEMPORARY TABLE acctmp
ON COMMIT PRESERVE ROWS
AS SELECT * FROM tso_fahrplan_fluss f
WHERE 1=1 AND exists ( select *
from tso_fahrplan_daten d
where d.tso_fahrplan_fluss_id = f.tso_fahrplan_fluss_id AND cdate>(trunc(sysdate)-' || keepDays || '))';
execute immediate 'TRUNCATE table tso_fahrplan_fluss DROP STORAGE';
execute immediate 'INSERT INTO tso_fahrplan_fluss
SELECT * FROM acctmp';
execute immediate 'TRUNCATE TABLE acctmp';
execute immediate 'DROP TABLE acctmp';
--SELECT * FROM acctmp;
dbms_output.put_line ('tso_fahrplan_daten handling') ;
execute immediate 'LOCK TABLE tso_fahrplan_daten IN EXCLUSIVE MODE';
execute immediate 'CREATE GLOBAL TEMPORARY TABLE acctmp2
ON COMMIT PRESERVE ROWS
AS SELECT * FROM tso_fahrplan_daten f
WHERE 1=1 AND cdate > (trunc(sysdate)-' || keepDays || ')';
execute immediate 'TRUNCATE table tso_fahrplan_daten DROP STORAGE';
execute immediate 'INSERT INTO tso_fahrplan_daten
SELECT * FROM acctmp2';
execute immediate 'TRUNCATE TABLE acctmp2';
execute immediate 'DROP TABLE acctmp2';
dbms_output.put_line ('tso_fahrplan handling') ;
execute immediate 'LOCK TABLE tso_fahrplan IN EXCLUSIVE MODE';
execute immediate 'CREATE GLOBAL TEMPORARY TABLE acctmp3
ON COMMIT PRESERVE ROWS
AS SELECT * FROM tso_fahrplan f
WHERE 1=1 AND f.cdate > (trunc(sysdate)-' || keepDays || ')';
execute immediate 'TRUNCATE table tso_fahrplan DROP STORAGE';
execute immediate 'INSERT INTO tso_fahrplan
SELECT * FROM acctmp3';
execute immediate 'TRUNCATE TABLE acctmp3';
execute immediate 'DROP TABLE acctmp3';
END;
COMMIT ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment