问题
I am trying to write a Procedure which essentially drops partitions from several tables which are stored in multiple schemas. End goal is to then create a dbms scheduler which will run this procedure every day and check for partitions that hold data older than 6 months. How to add functionality of looking for partitions across multiple schemas ?
I have created a Procedure which drops a partition only from a specific table.
PROCEDURE purge_ops_log_range_parts IS
BEGIN
FOR partition_rec IN (SELECT partition_name
,high_value
FROM user_tab_partitions
WHERE table_name =
'OPSWIRE_LOG_RANGE_PARTS')
LOOP
IF SYSDATE >= add_months(to_date(substr(partition_rec.high_value
,12
,19)
,'YYYY-MM-DD HH24:MI:SS')
,6)
THEN
execute_immediate('ALTER TABLE OPS_LOG_RANGE_PARTS DROP PARTITION ' ||
partition_rec.partition_name);
END IF;
END LOOP;
END purge_ops_log_range_parts;
Output is deleting partition from a specific table only however it does not look for multiple tables in various schemas.
回答1:
Use the DBA_TAB_PARTITIONS or ALL_TAB_PARTITIONS views instead of USER_TAB_PARTITIONS. The former two views contain a TABLE_OWNER (i.e. schema) column which should help you accomplish your goal.
You can then parameterize your procedure to take the owner and table names as parameters:
PROCEDURE purge_ops_log_range_parts(pinOwner IN VARCHAR2,
pinTable_name IN VARCHAR2)
IS
BEGIN
FOR partition_rec IN (SELECT partition_name
,high_value
FROM DBA_TAB_PARTITIONS
WHERE TABLE_OWNER = pinOwner AND
table_name = pinTable_name)
LOOP
IF SYSDATE >= add_months(to_date(substr(partition_rec.high_value
,12
,19)
,'YYYY-MM-DD HH24:MI:SS')
,6)
THEN
execute_immediate('ALTER TABLE ' || pinOwner || '.' ||
pinTable_name || ' DROP PARTITION ' ||
partition_rec.partition_name);
END IF;
END LOOP;
END purge_ops_log_range_parts;
Best of luck.
来源:https://stackoverflow.com/questions/53936930/how-to-drop-partitions-from-tables-across-multiple-schemas