oracle中连接条件中的case语句

时间:2016-01-26 10:26:09

标签: oracle join dynamic-sql case-when

我有下面的程序,其中我在sql查询中动态使用db链接,我有情况我试图在连接条件中使用case statement因为依赖于db链接我必须在连接条件中使用表但我收到的错误为ORA-06512 "missing right parenthesis"

PROCEDURE "EXT_I_IN"(IN_I_ID IN NUMBER DEFAULT 0) AS

    err_code VARCHAR(100);
    err_msg VARCHAR(100);         
     l_sql VARCHAR2(5000);
     l_sql1 VARCHAR2(5000);

    BEGIN       

    FOR I_ROW IN I_CUR
    LOOP

    l_sql:='INSERT INTO I_IN(ID,DB_LINK)
    Select DISTINCT(INC.ID),:IN_DB_LINK  FROM
    IN_I@'||IN_DB_LINK||' IC JOIN 
    (CASE WHEN :DB_NAME = ''IDB'' THEN IC.INC@'||IN_DB_LINK||' ELSE IN.INC@'||IN_DB_LINK||' END) IDL
    ';

    DBMS_OUTPUT.PUT_LINE('lsql' || l_sql);
    execute immediate l_sql using IN_DB_LINK;

    END EXT_I_IN;'

1 个答案:

答案 0 :(得分:2)

看起来很奇怪,你不能在JOIN中使用CASE:

 (CASE WHEN :DB_CONNECTION_NAME = ''FONIC_RETAIL'' THEN RATOR_FONIC_CDR.INVOICE_DETAIL_LINE@'||IN_DB_LINK||' ELSE RATOR_CDR.INVOICE_DETAIL_LINE@'||IN_DB_LINK||' END) IDL

但您可以在构建查询时检查此情况:

l_sql:='INSERT INTO TEMP_TOPUP_DRC_IN(ID,INVOICE_DETAIL_LINE_ID,STATUS,BILLING_GROUP_ID,DB_LINK)
    Select DISTINCT(INC.ID),INC.INVOICE_DETAIL_LINE_ID,INC.STATUS,IDL.BILLING_GROUP_ID,:IN_DB_LINK  FROM
    IN_NODE_CHARGE@'||IN_DB_LINK||' INC JOIN ';
CASE WHEN IN_DB_LINK = 'FONIC_RETAIL' THEN l_sql := l_sql || ' RATOR_FONIC_CDR.INVOICE_DETAIL_LINE@'||IN_DB_LINK;
     ELSE l_sql := l_sql || ' RATOR_CDR.INVOICE_DETAIL_LINE@'||IN_DB_LINK;
END CASE;
l_sql := ' IDL ON INC.INVOICE_DETAIL_LINE_ID = IDL.ID
    JOIN ACCOUNT_PAYMENT@'||IN_DB_LINK||' AP ON IDL.BILLING_RECORD_ID = AP.ID
      WHERE AP.PAYMENT_TYPE_ID = 24
      AND INC.STATUS IN (1,3,6,2,4,14,20,21,100,101,102)';