ORA-14308:分区绑定元素必须是以下之一:字符串,日期时间或间隔文字,数字或NULL

时间:2019-06-27 11:12:32

标签: oracle plsql dynamic-sql database-partitioning

CREATE OR REPLACE PROCEDURE pro_alter_partition(INTERFACE_ID VARCHAR2) 
    AS
    L_partition_name            VARCHAR2(100);
    L_subpartition_name            VARCHAR2(100);
    L_subpartition_inc            NUMBER;
    BEGIN

        SELECT MAX(partition_name), MAX(subpartition_name) 
            INTO L_partition_name, L_subpartition_name
                FROM dba_tab_subpartitions 
                    WHERE table_name = UPPER('jcff_int_inb_staging_data');

        dbms_output.put_line(L_subpartition_name);

        L_subpartition_inc := to_number(substr(L_subpartition_name,9))+1;
        L_subpartition_name := substr(L_subpartition_name,1,8)||L_subpartition_inc;

        dbms_output.put_line(L_subpartition_inc);
        dbms_output.put_line(L_subpartition_name);

        EXECUTE IMMEDIATE 'ALTER TABLE JDACUST.JCFF_INT_INB_STAGING_DATA
            MODIFY partition '||L_partition_name ||
                ' ADD subpartition '||L_subpartition_name||' values ('||INTERFACE_ID||')';

    END pro_alter_partition;
    /

它不应引发错误并应更改分区

1 个答案:

答案 0 :(得分:1)

INTERFACE_ID是字符串文字。因此,需要用引号引起来。仅因为您使用的是动态SQL,才需要对它们进行转义:

EXECUTE IMMEDIATE 'ALTER TABLE JDACUST.JCFF_INT_INB_STAGING_DATA
    MODIFY partition '||L_partition_name ||
        ' ADD subpartition '||L_subpartition_name||' values ('''||INTERFACE_ID||''')';

动态SQL很难,因为它将编译错误转换为运行时错误。为了发现错误,我们需要像编译器一样思考。使自己变得容易,并使用变量来汇编可执行字符串。您可以禁用该变量以查看实际执行的语句,这更易于调试:

stmt := 'ALTER TABLE JDACUST.JCFF_INT_INB_STAGING_DATA
        MODIFY partition '||L_partition_name ||
            ' ADD subpartition '||L_subpartition_name||' values ('''||INTERFACE_ID||''')';

dbms_output.put_line(stmt);

EXECUTE IMMEDIATE stmt;