oracle11G开始了提供了自动分区的功能,当分区多了后,要删除某些分区的话,可写个job来定时清理。
select * from User_Tab_Partitions
where table_name like talbename(表名);
这条语句是查talbename这个 表目前的分区明细。
根据这条语,我们可以删除所想的分区
procedure pro_temp is
v_num NUMBER := 0;
v_sql VARCHAR2(100) := '';
begin
pro_Log('pro_temp',
'删除temp表7天前的分区:执行开始',
sysdate,
'1');
--先查出当前表的分区数量,除去建表时的默认分区
select count(1)
into v_num
from User_Tab_Partitions
WHERE table_name = 'CCW_TEMP'
and partition_name <> 'P20150909';
--当分区数量大于7,删除7天前的分区
if v_num > 7 then
FOR i IN 1 .. (v_num - 7) LOOP
select (SELECT 'alter table ' || t.table_name || ' drop partition ' ||
t.partition_name v_sql
FROM User_Tab_Partitions t
WHERE t.table_name = 'CCW_TEMP'
AND t.partition_position = 2)
into v_sql
from dual;
EXECUTE IMMEDIATE v_sql;
dbms_output.put_line('成功删除第' || (7 + i) || '天的分区');
END LOOP;
end if;
exception
when others then
rollback;
pro_Log(pro_temp',
'删除temp表7天前的分区:执行异常',
sysdate,
'3');
end pro_temp;
--然后新建一个job,每天执行
declare -- Local variables here i integer; begin -- Test statements here dbms_job.submit(i,'pro_temp;',sysdate,'TRUNC(sysdate)+1+2/(24)'); commit; end;
附带删除job的语句
declare -- Local variables here i number; begin -- Test statements here select job into i from user_jobs a where what = 'pro_temp;'; sys.dbms_job.remove(i); commit; end;