无法在动态sql中执行with子句

时间:2017-12-11 10:23:28

标签: oracle plsql

我尝试使用动态SQL执行以下语句但我得到此异常

  

" ORA-00911:   ORA-06512"上的字符无效。

当我删除分号(;)时,查询会运行,但需要很长时间,但无法显示输出。

如何清除此问题?我在哪里做错了?如果是的话请指导我。

以下是我的代码:

DECLARE    
   mapping_rule     VARCHAR2 (10000)
      := 'SALES_REVIEW.NET_VALUE-SALES_REVIEW.GROSS_VALUE+SALES_REVIEW_TABLE.PLNT';
   v_mapp_rule      VARCHAR2 (10000);    
   v_mapp_rule_1    VARCHAR2 (10000);    
   v_chk_flag       CHAR (1) := 'Y';
   v_mapping_rule   VARCHAR2 (10000);
   v_str            VARCHAR2 (30000);
BEGIN
  <<dest_stmt>>
   DBMS_OUTPUT.put_line (v_chk_flag);

   IF v_chk_flag = 'Y'
   THEN
      v_mapping_rule := mapping_rule;
   ELSE
      v_mapping_rule := v_mapp_rule_1;
   END IF;

   v_str :=
      'WITH sel_col (rowno, mapp_rule)
        AS (    SELECT ROWNUM rowno,
                       REGEXP_SUBSTR (  ''' || v_mapping_rule
      || ''',''([+--*!@/#$%^&()=<>,?]+|[A-Z0-9_.'''']+|\s+)'',
                          1,
                          LEVEL)
                          mapp_rule
                  FROM DUAL
            CONNECT BY REGEXP_SUBSTR ('''
      || v_mapping_rule
      || ''',''([+--*!@/#$%^&()=<>,?]+|[A-Z0-9_.'''']+|\s+)'',
                          1,
                          LEVEL)
                          IS NOT NULL)
     SELECT listagg (
               CASE WHEN B.MAPP_RULE IS NOT NULL THEN B.MAPP_RULE 
                    ELSE D.MAPP_RULE END,
               '''')
            WITHIN GROUP (ORDER BY d.rowno)
       FROM RRR_PROCESS_DTLS a
            JOIN RRR_PROCESS_MAPPING_DTLS b
               ON A.PROCESS_ID = B.PROCESS_ID 
                  AND A.COMPANY_ID = B.COMPANY_ID
            JOIN RRR_DEST_TABLE_DTLS C
               ON A.DEST_TABLE = C.TABLE_ID 
                  AND A.COMPANY_ID = C.COMPANY_ID
            RIGHT JOIN sel_col d
               ON CONCAT (CONCAT (C.TABLE_NAME, ''.''), B.DEST_COLUMN) =
                     d.mapp_rule
                  AND A.PROCESS_ID = 12 
                  AND A.COMPANY_ID = 2
   ORDER BY d.rowno;';

   DBMS_OUTPUT.put_line (v_str);

   EXECUTE IMMEDIATE v_str into v_mapp_rule_1 ;

   DBMS_OUTPUT.put_line ('bf ' || v_mapp_rule_1);

   IF v_mapp_rule_1 IS NOT NULL
   THEN
      v_chk_flag := 'N';
      GOTO dest_stmt;
   END IF;    

   DBMS_OUTPUT.put_line (v_mapp_rule_1);
EXCEPTION
   WHEN OTHERS
   THEN
      DBMS_OUTPUT.PUT_LINE (
            SQLCODE
         || ' -ERROR- '
         || SQLERRM
         || ' on '
         || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
END;

0 个答案:

没有答案