如何从跨多个架构的表中删除分区? [英] How to drop partitions from tables across multiple schemas?

查看:89
本文介绍了如何从跨多个架构的表中删除分区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个过程,该过程本质上是从存储在多个模式中的几个表中删除分区.最终目标是创建一个dbms调度程序,该程序将每天运行此过程,并检查保存了6个月以上数据的分区.如何添加跨多个模式查找分区的功能?

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.

推荐答案

使用DBA_TAB_PARTITIONSALL_TAB_PARTITIONS视图而不是USER_TAB_PARTITIONS.前两个视图包含一个TABLE_OWNER(即架构)列,该列应有助于您实现目标.

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;

好运.

这篇关于如何从跨多个架构的表中删除分区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆