0

我有以下过程,我将 db 链接作为参数传递并创建动态 sql 语句。在执行该过程时,我收到错误消息ORA-00904: "DB_CONNECTION_NAME": invalid identifier。我已经声明了变量,但我仍然收到此错误。

CREATE OR REPLACE PROCEDURE "EXT_SDR_RECEIVED"(in_db_link IN VARCHAR2)
AS
  last_sm_id         NUMBER := 0;
  last_capt_date     DATE;
  l_sql              VARCHAR2(5000);
  db_connection_name VARCHAR2(100);
  BEGIN

    SELECT db_link INTO db_connection_name
    FROM rator_monitoring_configuration.db_connection
    WHERE db_link = in_db_link;

    --DELETE DATA FROM TEMP_SDR_RECEIVED
    DELETE FROM temp_sdr_received WHERE create_date < SYSDATE - 7;

    -- first retrieve the last id (of the newest record) which has been imported at last extraction
    SELECT last_task_id INTO last_sm_id 
    FROM capturing WHERE db_table = 'TEMP_SDR_RECEIVED';

    SELECT capturing_date INTO last_capt_date
    FROM capturing WHERE db_table = 'TEMP_SDR_RECEIVED';

    dbms_output.PUT_LINE('DB' || db_connection_name);
    -- retrieve all new records from remote SDR_O2 table and insert it into TEMP_SDR_RECEIVED where ID is greater than LAST_SM_ID

    l_sql := 'INSERT INTO TEMP_SDR_RECEIVED(ID,RATING_CODE,A_NUMBER,CREATE_DATE,VOUCHER_ATTEMPT_ID,RATOR_BRAND_ID,BRAND_ID,STATUS_DESCRIPTION,ACCOUNT_PAYMENT_ID,SUBSCRIPTION_ID,DB_LINK)
              SELECT SD.ID,SD.RATING_CODE,SD.A_NUMBER,to_date(substr(SD.ID, 1, 8), ''YYYYMMDD''),VA.ID,VA.BRAND_ID,BR.BRAND_ID,VA.STATUS_DESCRIPTION,VA.ACCOUNT_PAYMENT_ID,VA.SUBSCRIPTION_ID,DB_CONNECTION_NAME
                FROM SDR_O2@' || db_connection_name || ' SD
                JOIN VOUCHER_ATTEMPT@' || db_connection_name || ' VA
                  ON SD.ID = VA.SDR_ID,
                     RATOR_MONITORING_CONFIGURATION.BRAND BR
               WHERE VA.BRAND_ID IS NOT NULL
                 AND BR.RATOR_BRAND_ID = VA.BRAND_ID
                 AND SD.RATING_CODE=''VOUCHER'' 
                 AND VA.STATUS_DESCRIPTION = ''USSD voucher''
                 AND SD.ID > LAST_SM_ID';

    EXECUTE IMMEDIATE l_sql;

  END ext_sdr_received;
4

1 回答 1

1

DB_CONNECTION_NAME在动态查询的选择部分中引用(查看 VA.SUBSCRIPTION_ID)。您的 3 个表中的任何一个都有该列吗?我怀疑不是。

我怀疑您想改为选择DB_CONNECTION_NAME变量中的值。为此,请更改SELECT动态查询中的最后一部分,如下所示:

'...,VA.SUBSCRIPTION_ID, ''' || DB_CONNECTION_NAME || '''
...

您可能还想了解如何execute immediate支持参数绑定,以便在可能的情况下避免编写这种丑陋的字符串连接代码。

另外,我注意到您正在混合连接符号。那是自找麻烦。坚持ANSI JOIN语法。

于 2015-12-18T13:45:31.647 回答