Oracle11g定時刪除資料表分割槽...
CREATE OR REPLACE PROCEDURE PROC_DROP_PATITION_WELLDIAGRAM(beforeDays NUMBER) As
v_SqlExec VARCHAR2(2000);
v_err_num NUMBER;
v_err_msg VARCHAR2(100);
cursor cursor_cpu_info_part is
select t.SUBOBJECT_NAME partition_name
from user_objects t
where object_name = upper('WELL_DIAGRAM')
and t.OBJECT_TYPE = 'TABLE PARTITION'
and t.GENERATED ='Y' --第一個分割槽也就是建立表時候建立的第一個分割槽是不允許被刪除的,oracle報錯,自動GENERATED 的分割槽是可以刪除的
and t.CREATED < sysdate - 30; -- 尋找30天之前的
record_cpu_info_oldpart cursor_cpu_info_part%rowType;
BEGIN
open cursor_cpu_info_part;
loop
fetch cursor_cpu_info_part
into record_cpu_info_oldpart;
exit when cursor_cpu_info_part%notfound;
--刪除WELL_DIAGRAM表分割槽
v_SqlExec := 'ALTER TABLE WELL_DIAGRAM DROP PARTITION ' ||
record_cpu_info_oldpart.partition_name;
dbms_output.put_line('刪除WELL_DIAGRAM表分割槽=' || v_SqlExec);
DBMS_Utility.Exec_DDL_Statement(v_SqlExec);
end loop;
close cursor_cpu_info_part;
EXCEPTION
WHEN OTHERS THEN
v_err_num := SQLCODE;
v_err_msg := SUBSTR(SQLERRM, 1, 100);
dbms_output.put_line('MALS_NM_DROP_PARTITION_PROC執行出現異常,錯誤碼=' ||
v_err_num || '錯誤描述=' || v_err_msg);
END PROC_DROP_PATITION_WELLDIAGRAM;
CREATE OR REPLACE PROCEDURE Repair_WELLDIAGRAM
is
begin
proc_drop_patition_welldiagram(1);
execute immediate 'alter table WELL_DIAGRAM enable row movement';
execute immediate 'alter table WELL_DIAGRAM shrink space';
end;
declare
jobno number;
begin
dbms_job.submit(
jobno,
what => 'Clear_WELLDIAGRAM;', --pro_clear_log為儲存過程的名稱
next_date=>to_date('21-01-2022 13:30:00','dd-mm-yyyyhh24:mi:ss'),--引數指識何時將執行這個工作
Interval => 'TRUNC(sysdate) + 1 +12/(24)' --定義時間間隔 5個小時
);
commit;
end;