Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Delete Rows from table using pl/sql

674481Mar 9 2010 — edited Mar 11 2010
I am working on deleting rows from this table (my.table_job_status) that are old and with a lot of lines. What are want to accomplish is to select those lines with it job# and have them deleted in seperate file created. The point is I will like it to be dynamically as mush as possible.

For example, if I do something like this "select * from my_table where num_lines > 50000", I will like for the result to be place in a sql file "my_sql" and executed. Like execute immediate my_sql.

This is what I am working on:

cursor my_cursor is
select table_name, jobid, submit_time, starttime, endtime, status, num_lines from
my.table_job_status
where endtime < sysdate - 60 AND num_lines > 1000000;
Begin
for del_r in my_cursor loop
if --the above is true THEN
my_sql:='delete from' || my.table_job_status
execute immediate my_sql;
end if
end loop
exeption
when others then
if sqlcode != -942 then
raise;
end IF;
end;
commit;


As you can see below here, we are currently deleting tables in the same manor, but I don't know how to accomplish this in deleting rows from tables:

cursor obj_cursor is
select
object_name,
object_type,
owner
from
dba_objects
where
owner='MY'
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;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2010
Added on Mar 9 2010
12 comments
4,317 views