Home » SQL & PL/SQL » SQL & PL/SQL » PL sql block (oracle 11g)
PL sql block [message #620273] Wed, 30 July 2014 15:30 Go to next message
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 #620274 is a reply to message #620273] Wed, 30 July 2014 15:36 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/

You DROP object & DELETE rows.

It is unprofessional & data is not normalized when date is part of table_name & in such a format as to be not in normal collating sequence; such as 0730

You need to write SQL which constructs the desired DROP statements.
Re: PL sql block [message #620276 is a reply to message #620274] Wed, 30 July 2014 15:55 Go to previous messageGo to next message
shaikhaltaf05@gmai.com
Messages: 2
Registered: July 2014
Location: US
Junior Member
Thanks!!

can you please help me with the sql to do this task
Re: PL sql block [message #620279 is a reply to message #620276] Wed, 30 July 2014 20:05 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
This forum is not a free coding service.
I would expect that someone at your company can provide the necessary guidance if you ask around nicely.

IMO, who ever came up with these table_names should be at the very least be(re)trained, if not terminated for incompetence.
Almost without exception temporary tables are rarely required in Oracle database.
Re: PL sql block [message #620285 is a reply to message #620273] Wed, 30 July 2014 23:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Regarding the question:
EXECUTE IMMEDIATE

But why don't you do it in pure SQL?

Re: PL sql block [message #620297 is a reply to message #620273] Thu, 31 July 2014 01:42 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous message
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.
Previous Topic: SQL query combination
Next Topic: Help Required: Parsing String Collection or Concatenated String
Goto Forum:
  


Current Time: Fri Apr 19 15:46:24 CDT 2024