作为分区名称的存储过程输入在 oracle 中不起作用

Stored procedure input as partition name not working in oracle

这是我的存储过程,输入的是要交换的分区名。但是,查询无法识别我输入的名称 ('DATA_EXCHANGE_PAYLOAD_20160630'),但该分区确实存在于数据库中。

    CREATE
        OR REPLACE PROCEDURE exchange_partitions (partition_name IN VARCHAR2) AS

    BEGIN
        EXECUTE IMMEDIATE 
    '

    ALTER TABLE BUCLM_ADAPTERDB.DATA_EXCHANGE_PAYLOAD EXCHANGE PARTITION partition_name
        WITH TABLE BUCLM_ADAPTERDB.DATA_EXCHANGE_PAYLOAD_TEMP
    ';
    END;

    exec exchange_partitions('DATA_EXCHANGE_PAYLOAD_20160630');

您的 partition_name 参数未被使用。 alter table 语句实际上是在查找字符串 partition_name。这不是你想要的。

要修复它,请使用连接运算符 ||:

partition_name 的值连接到动态 SQL 中
CREATE OR REPLACE PROCEDURE exchange_partitions (partition_name IN VARCHAR2) AS
BEGIN
    EXECUTE IMMEDIATE 'ALTER TABLE BUCLM_ADAPTERDB.DATA_EXCHANGE_PAYLOAD
                       EXCHANGE PARTITION ' || partition_name || ' WITH
                       TABLE BUCLM_ADAPTERDB.DATA_EXCHANGE_PAYLOAD_TEMP';
END;
EXECUTE IMMEDIATE  'ALTER TABLE BUCLM_ADAPTERDB.DATA_EXCHANGE_PAYLOAD 
                    EXCHANGE PARTITION :1  WITH TABLE 
                    BUCLM_ADAPTERDB.DATA_EXCHANGE_PAYLOAD_TEMP'
                    USING partition_name;