Home » Infrastructure » Unix » How to keep last four tables in a schema?
How to keep last four tables in a schema? [message #130794] Tue, 02 August 2005 10:44 Go to next message
humair77
Messages: 13
Registered: August 2005
Junior Member
I need to write a shell script that can keep last four tables and rest of them can be dropped. Table name will be "table_name_yymmdd"

Please help me out !!

Thanks,
Humair
Re: How to keep last four tables in a schema? [message #130818 is a reply to message #130794] Tue, 02 August 2005 12:38 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator

oracle@mutation#crt
create table myable_20050731 (id number)
create table myable_20050730 (id number)
create table myable_20050729 (id number)
create table myable_20050728 (id number)
create table myable_20050727 (id number)
create table myable_20050726 (id number)
create table myable_20050725 (id number)
create table myable_20050724 (id number)
create table myable_20050723 (id number)
create table myable_20050722 (id number)

PL/SQL procedure successfully completed.

oracle@mutation#dt
These are the tables found
MYABLE_20050722
MYABLE_20050723
MYABLE_20050724
MYABLE_20050725
MYABLE_20050726
MYABLE_20050727
MYABLE_20050728
MYABLE_20050729
MYABLE_20050730
MYABLE_20050731
These are the last 4 tables (based on date), to be deletd
MYABLE_20050731
MYABLE_20050730
MYABLE_20050729
MYABLE_20050728

PL/SQL procedure successfully completed.

oracle@mutation#cat crt
#!/usr/bin/bash
sqlplus -s scott/tiger <<EOF
declare
i number:=1;
begin
for mag in 1..10 loop
i:=i+1;
execute immediate('create table myable_'||to_char(sysdate-i,'YYYYMMDD') ||' (id number)');
DBMS_OUTPUT.PUT_LINE ('create table myable_'||to_char(sysdate-i,'YYYYMMDD') ||' (id number)');
end loop;
end;
/
exit;
EOF

oracle@mutation#cat dt
sqlplus -s scott/tiger <<EOF
declare
cursor c1 is select table_name from cat;
cursor c2 is
        select table_name from
        (select table_name from cat
        order by to_date(substr(table_name,8,length(table_name)),'YYYYMMDD') desc)
        where rownum < 5;
begin
DBMS_OUTPUT.PUT_LINE ('These are the tables found');
for mag in c1 loop
        exit when c1%notfound;
        DBMS_OUTPUT.PUT_LINE (mag.table_name);
        end loop;
DBMS_OUTPUT.PUT_LINE ('These are the last 4 tables (based on date), to be deletd');
for mag2 in c2 loop
        exit when c2%notfound;
        DBMS_OUTPUT.PUT_LINE (mag2.table_name);
        execute immediate('drop table '||mag2.table_name||' cascade constraints');
        end loop;
end;
/
exit
EOF
Re: How to keep last four tables in a schema? [message #130819 is a reply to message #130818] Tue, 02 August 2005 12:40 Go to previous message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
Here i delete the 4 tables and keep the rest.
Exactly opposite to what you have asked!.
Just reverse the conditions.
Previous Topic: Oracle 9i installation has linking problem on AIX machine
Next Topic: how to start OMS at solaris unix env?
Goto Forum:
  


Current Time: Sat Apr 20 03:51:35 CDT 2024