SELECT a.table_name FROM user_tables a where a.table_name LIKE 'T20%' AND trunc(SYSDATE) - to_date(SUBSTR(a.table_name, 2),'yyyymmdd') >= 20 ; 拼动态sql,然后drop table。之后加个定时任务
这个叫 oracle plsql 定时器,Jobs 循环遍历游标 create or replace procedure test() as --存储过程名,建立如果存在替换, Cursor cursor is select table_name from user_objects; --声明游标是用户名 name varchar(200); begin --存储过程开始 for name in cursor LOOP --循环体 begin --循环开始 if sysdate - to_timestamp(substr(name, 2, 8) ,'yyyymmdd') > 20 then --如果当前时间比数据库表中时间大了 20 天 begin drop name; --删除表 end; end if; end; --循环结束 end LOOP; --循环结束end test; --存储过程结束我是这样解决的,但后来发现,那个表名称没做处理。。应该做一下like'T' 的处理的
--------创建存储过程 create or replace procedure pro_del is v_tab varchar2(50); v_cut number; v_date date; v_sql varchar2(200); v_cur binary_integer;---注意这里不是游标 v_cut2 number; begin ---得到当前天20天之前的日期 如当前天是 2011-02-21 那么前20是 2011-02-01 select sysdate-20 into v_date from dual; --dbms_output.put_line(to_char(v_date,'yyyyMMdd')); v_tab:='T'||to_char(v_date,'yyyyMMdd'); --构造删除语句 v_sql:='drop table '||v_tab; --查询是否存在表名为 v_tab的表,如果存在则v_cut大于0; select count(table_name) into v_cut from user_tables where table_name=v_tab; v_cur:=dbms_sql.open_cursor;---打开游标 if v_cut >0 then dbms_sql.parse(v_cur,v_sql,dbms_sql.native);--执行删除语句 else select count(table_name) into v_cut2 from user_tables where table_name='T20120127'; if v_cut2 >0 then dbms_sql.parse(v_cur,'drop table T20120127',dbms_sql.native);--执行删除语句 end if; end if;
dbms_sql.close_cursor(v_cur); end; / ----创建jobdeclare job number; begin ---trunc(sysdate)+8/24 每天的8点执行存储过程pro_del sysdate+1/1440 --sys.dbms_job.submit(job,'pro_del;',sysdate-1,'trunc(sysdate+1)+(20*60+49) /(24*60)'); sys.dbms_job.submit(job,'pro_del;',sysdate,'sysdate+1/1440'); commit; end; /---运行job ---- select job from dba_jobs where what='pro_del;'; 可以查找到 job的值,我本机上是40 begin sys.dbms_job.run(40); end;
FROM user_tables a
where a.table_name LIKE 'T20%'
AND trunc(SYSDATE) - to_date(SUBSTR(a.table_name, 2),'yyyymmdd') >= 20
;
拼动态sql,然后drop table。之后加个定时任务
create or replace procedure test() as --存储过程名,建立如果存在替换,
Cursor cursor is select table_name from user_objects; --声明游标是用户名
name varchar(200); begin --存储过程开始 for name in cursor LOOP --循环体 begin --循环开始 if sysdate - to_timestamp(substr(name, 2, 8) ,'yyyymmdd') > 20 then --如果当前时间比数据库表中时间大了 20 天 begin drop name; --删除表 end; end if; end; --循环结束 end LOOP; --循环结束end test; --存储过程结束我是这样解决的,但后来发现,那个表名称没做处理。。应该做一下like'T' 的处理的
--------创建存储过程
create or replace procedure pro_del
is v_tab varchar2(50);
v_cut number;
v_date date;
v_sql varchar2(200);
v_cur binary_integer;---注意这里不是游标
v_cut2 number;
begin
---得到当前天20天之前的日期 如当前天是 2011-02-21 那么前20是 2011-02-01
select sysdate-20 into v_date from dual;
--dbms_output.put_line(to_char(v_date,'yyyyMMdd'));
v_tab:='T'||to_char(v_date,'yyyyMMdd');
--构造删除语句
v_sql:='drop table '||v_tab;
--查询是否存在表名为 v_tab的表,如果存在则v_cut大于0;
select count(table_name) into v_cut from user_tables where table_name=v_tab;
v_cur:=dbms_sql.open_cursor;---打开游标
if v_cut >0 then
dbms_sql.parse(v_cur,v_sql,dbms_sql.native);--执行删除语句
else
select count(table_name) into v_cut2 from user_tables where table_name='T20120127';
if v_cut2 >0 then
dbms_sql.parse(v_cur,'drop table T20120127',dbms_sql.native);--执行删除语句
end if;
end if;
dbms_sql.close_cursor(v_cur);
end;
/
----创建jobdeclare
job number;
begin
---trunc(sysdate)+8/24 每天的8点执行存储过程pro_del sysdate+1/1440
--sys.dbms_job.submit(job,'pro_del;',sysdate-1,'trunc(sysdate+1)+(20*60+49) /(24*60)');
sys.dbms_job.submit(job,'pro_del;',sysdate,'sysdate+1/1440');
commit;
end;
/---运行job ---- select job from dba_jobs where what='pro_del;'; 可以查找到 job的值,我本机上是40
begin
sys.dbms_job.run(40);
end;
楼主可以试试,是否符合你的要求。