Use PLSQL to delete tables dynamically
674481Mar 11 2010 — edited Mar 12 2010What I want to do is drop tables from dba_objects base on strings from another table called (ita.tpis32_job_status) using scrip like this from John G.’s code depot as you can see below. So I created the second script. What I want the second script to is append the jobid from (ita.tpis32_job_status ) in the third script and find all tables with such jobid.
To further explain what I mean, I listed example of jobid In the third script, I found all the jobids based on dates that I want associated with the tables to be deleted in dba_ojects. What want to do is add the five digit job id to wk_jobstr in second script to find all tables and views with such job id and have them dropped. Your help is really appreciated.
Example script 1
declare
v_str varchar2(200);
begin
for r_c1 in (select * from user_tables) loop
v_str:= 'delete '||r_c1.table_name;
dbms_output.put_line(v_str);
--execute immediate v_str;
end loop;
end;
/
Main - second script
declare
cursor obj_cursor is
select
object_name,
object_type,
owner
from
dba_objects
where
owner='ITA'
and object_type in ('TABLE','VIEW')
and object_name like wk_jobstr;
WK_SQL VARCHAR2(500);
WK_JOBSTR VARCHAR2(500);
out_f utl_file.file_type;
BEGIN
wk_jobstr:='JOB_' || ltrim(to_char(wk_job)) || '%';
for wk_rec in obj_cursor loop
if wk_rec.object_type='TABLE' then
wk_sql:='drop table ' || wk_rec.object_name;
--EXECUTE IMMEDIATE WK_SQL;
IF 1=2 THEN
dbms_sql.parse(v_cursor, wk_sql, dbms_sql.native);
ret_val:=dbms_sql.execute(v_cursor);
END IF;
SP_SHOW_sql(wk_sql,'wk_sql');
dbms_output.put_line(wk_sql);
if print_yn>0 then
dbms_output.put_line('table ' || wk_rec.object_name || ' deleted');
end if;
elsif wk_rec.object_type='VIEW' then
wk_sql:='drop view ' || wk_rec.object_name;
--EXECUTE IMMEDIATE WK_SQL;
IF 1=2 THEN
dbms_sql.parse(v_cursor, wk_sql, dbms_sql.native);
ret_val:=dbms_sql.execute(v_cursor);
END IF;
if print_yn>0 then
dbms_output.put_line('view ' || wk_rec.object_name || ' deleted') ;
end if;
end if;
end loop;
exception
when others then
if sqlcode != -942 then
raise;
end if;
end;
commit;
Third script:
set serveroutput on
declare
2 cursor del_cursor is
3 select table_name, jobid, submit_time, starttime, endtime, status, num_line
s from
4 ita.tpis32_job_status where endtime < '10-MAY-05' AND num_lines > 1000000;
5 del_sql varchar2(500);
6 BEGIN
7 -- display all records you are going to delete
8 for del_r in del_cursor loop
9 del_sql:='delete from ita.tpis32_job_status where jobid =' || del_r.jobid;
10 dbms_output.put_line('delete from ita.tpis32_job_status where jobid =' || d
el_r.jobid);
11 end loop;
12 -- delete records
13 --delete from ita.tpis32_job_status where endtime < '10-MAY-05' AND num_lin
es > 100000000;
14 --commit;
15 END;
16 /
Results:
delete from ita.tpis32_job_status where jobid =28049
delete from ita.tpis32_job_status where jobid =28051
delete from ita.tpis32_job_status where jobid =28053
delete from ita.tpis32_job_status where jobid =27691
delete from ita.tpis32_job_status where jobid =27254
delete from ita.tpis32_job_status where jobid =27252
delete from ita.tpis32_job_status where jobid =21505
delete from ita.tpis32_job_status where jobid =21506
delete from ita.tpis32_job_status where jobid =25751