PL sql block [message #620273] |
Wed, 30 July 2014 15:30 |
|
shaikhaltaf05@gmai.com
Messages: 2 Registered: July 2014 Location: US
|
Junior Member |
|
|
Hi can any body help me with a plsql block to drop/delete the tables which are older than 30 days.
the name of the table is like this: testtable_3007---30 is date and 07 is month
thanks in advance
|
|
|
|
|
|
|
Re: PL sql block [message #620297 is a reply to message #620273] |
Thu, 31 July 2014 01:42 |
martijn
Messages: 286 Registered: December 2006 Location: Netherlands
|
Senior Member |
|
|
Maybe you can show us what you have already tried yourself.
Then we can guide you a bit better I guess.
|
|
|
Re: PL sql block [message #620597 is a reply to message #620297] |
Mon, 04 August 2014 09:36 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
We have a similar problem where we make backup of tables with the form "BKP_????' and I wrote this job to clean them up once a day. it get rids of any file older then 30 days. Feel free to use it. I run it using dba_job
CREATE OR REPLACE procedure CUSTOM.BKP_cleanup AUTHID CURRENT_USER as
cursor kill_tables is
select object_name
from ALL_OBJECTS
where created < trunc(sysdate) - 30
and object_type = 'TABLE'
AND OWNER = 'CUSTOM'
AND OBJECT_NAME LIKE 'BKP_%';
CHK_USER VARCHAR2(32);
BEGIN
-- Grab the current user from Oracle.
SELECT USER
INTO CHK_USER
FROM DUAL;
-- If it is not CUSTOM, no nothing
IF CHK_USER = 'CUSTOM' THEN
-- Loop through any tables that it found
FOR PNT IN KILL_TABLES LOOP
-- Get rid of the table.
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE CUSTOM.'||pnt.OBJECT_NAME;
EXCEPTION
-- if the drop fails for any reason, keep going
WHEN OTHERS THEN
NULL;
END;
END LOOP;
END IF;
END BKP_cleanup;
/
[Updated on: Mon, 04 August 2014 09:36] Report message to a moderator
|
|
|
Re: PL sql block [message #620602 is a reply to message #620597] |
Mon, 04 August 2014 09:53 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
But Bill, that WHEN OTHERS THEN NULL won't let you know what error didn't let Oracle drop the tables. OP might run into further trouble if he applies similar exception block with his limited knowledge.
|
|
|
Re: PL sql block [message #620605 is a reply to message #620602] |
Mon, 04 August 2014 09:58 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
WHile I normally don't use when others, i did in this instance because if the table doesn't drop it has to have a bad block which will go into the alert log or it's busy because someone is using it in which case it will get dropped the next day. It you don't have the DBA knowledge, it would be a good idea to expand the code to email a list of tables that it could not drop (using a GTT and utl_mail) and email it to myself.
|
|
|
Re: PL sql block [message #620611 is a reply to message #620605] |
Mon, 04 August 2014 10:07 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Yes, that's what I meant. To include something to do in the exception block. Like error logging, trigger an email etc.
But my serious concern is, if the table has referential constraint, it would error out next day as well. Such informtion would be required to know.
|
|
|