Oracle存储过程变量不起作用/无效的表名

时间:2018-08-16 18:11:10

标签: sql oracle plsql

我正在编写一个存储过程,以将数据从中间表插入到另一个表中。我使用动态sql语句,因为我必须在其中添加参数变量,因为它将用于多个表/不同数据。我是plsql的新手,这确实是我编写存储过程的第一步。当我将值硬编码时,它可以完美地工作。我尝试通过在第一行插入它们来添加数据库和表变量,但出现错误:

ORA-00903: invalid table name
ORA-06512: at "ECH_ETL_BATCH_ID.LOOKUP_TABLE_INSERT", line 29
ORA -06512: at line 14

有什么建议吗?

CREATE OR REPLACE PROCEDURE LOOKUP_TABLE_INSERT (
    P_SOURCE_DB      IN VARCHAR2,
    P_SOURCE_TABLE   IN VARCHAR2,
    P_TARGET_DB      IN VARCHAR2,
    P_TARGET_TABLE   IN VARCHAR2,
    P_COLUMN_NAME    IN VARCHAR2)
AS
    l_sql_statement   VARCHAR2 (1000);
BEGIN
    l_sql_statement := '
        INSERT INTO ' || P_TARGET_DB || '.' || P_TARGET_TABLE || '
                   (PKEY_SRC_OBJECT,
                    VERSION_SEQ,
                    TIMELINE_ACTION,
                    LAST_UPDATE_DATE,
                    SRC_ROWID,
                    HUB_STATE_IND,
                    ROLE_TP)
            SELECT MISSING_VALUES AS PKEY_SRC_OBJECT,
                   1              AS VERSION_SEQ,
                   0              AS TIMELINE_ACTION,
                   INSERT_TS      AS LAST_UPDATE_DATE,
                   ''3''          AS "SRC_ROWID",
                   1              AS "HUB_STATE_IND",
                   MISSING_VALUES AS ROLE_TP
              FROM ECH_ETL_BATCH_ID.Ref_Intermediate
             WHERE COLUMN_NM = :COLUMN_NAME
               AND LOOKUP_TBL_NM = :TARGET_TABLE';

    DBMS_OUTPUT.put_line (l_sql_statement);

    EXECUTE IMMEDIATE l_sql_statement USING P_COLUMN_NAME, P_TARGET_TABLE;

    COMMIT;
END;

这是一个硬编码版本,可以100%正常工作:

CREATE OR REPLACE PROCEDURE LOOKUP_TABLE_INSERT (
    P_SOURCE_DB      IN VARCHAR2,
    P_SOURCE_TABLE   IN VARCHAR2,
    P_TARGET_DB      IN VARCHAR2,
    P_TARGET_TABLE   IN VARCHAR2,
    P_COLUMN_NAME    IN VARCHAR2)
AS
    l_sql_statement   VARCHAR2 (1000);
BEGIN
    l_sql_statement varchar2(4000) := '
        INSERT INTO ECH_ETL_BATCH_ID.C_S_LU_PTY_ROLE_TP' || '
                   (PKEY_SRC_OBJECT,
                    VERSION_SEQ,
                    TIMELINE_ACTION,
                    LAST_UPDATE_DATE,
                    SRC_ROWID,
                    HUB_STATE_IND, ROLE_TP)
             SELECT MISSING_VALUES AS PKEY_SRC_OBJECT,
                    1              AS VERSION_SEQ,
                    0              AS TIMELINE_ACTION,
                    INSERT_TS      AS LAST_UPDATE_DATE,
                    ''3''          AS "SRC_ROWID",
                    1              AS "HUB_STATE_IND",
                    MISSING_VALUES AS ROLE_TP
               FROM ECH_ETL_BATCH_ID.Ref_Intermediate
              WHERE COLUMN_NM = ''ROLE_TP''
                AND LOOKUP_TBL_NM = ''C_S_LU_PTY_ROLE_TP''';

    DBMS_OUTPUT.put_line (l_sql_statement);

    EXECUTE IMMEDIATE l_sql_statement;

    COMMIT;
END;

这是我用来调用该过程的代码:

DECLARE
    P_SOURCE_DB      VARCHAR2 (200);
    P_SOURCE_TABLE   VARCHAR2 (200);
    P_TARGET_DB      VARCHAR2 (200);
    P_TARGET_TABLE   VARCHAR2 (200);
    P_COLUMN_NAME    VARCHAR2 (200);
BEGIN
    P_SOURCE_DB := NULL;
    P_SOURCE_TABLE := NULL;
    P_TARGET_DB := NULL;
    P_TARGET_TABLE := NULL;
    P_COLUMN_NAME := NULL;

    ECH_ETL_BATCH_ID.LOOKUP_TABLE_INSERT (
        P_SOURCE_DB      => ECH_ETL_BATCH_ID,
        P_SOURCE_TABLE   => Ref_Intermediate,
        P_TARGET_DB      => ECH_ETL_BATCH_ID,
        P_TARGET_TABLE   => C_S_LU_PTY_ROLE_TP,
        P_COLUMN_NAME    => ROLE_TP);
--rollback;
END;

0 个答案:

没有答案