存储过程中的REGEXP_SUBSTR函数

时间:2019-07-17 16:31:57

标签: oracle oracle11g

请帮助我解决此存储过程的PLS-00103错误:

'CREATE OR REPLACE PROCEDURE REPORTS_MV."Custom_Report" (l_cursor OUT sys_refcursor) AS v_sql CLOB; 
BEGIN 
v_sql := 'SELECT 
REGEXP_SUBSTR(Z2T.DISTRICT_NUMBER, '[^-]+') AS "District Number", REGEXP_SUBSTR(Z2T.DISTRICT_NUMBER, '[^-]+', 1, 2) AS "District Name", REGEXP_SUBSTR(Z2T.TERRITORY_NUMBER, '[^-]+') AS "Territory Number", REGEXP_SUBSTR(Z2T.TERRITORY_NUMBER, '[^-]+', 1, 2) AS "Territory Name"';

 v_sql := v_sql || ' FROM REPORTS_MV."STAGE-EMDEON_REDEMPTION" A ' || ' LEFT JOIN REPORTS_MV."STAGE-CARD_GROUP" CG ON A.GROUP_NUMBER = CG.GROUP_NUMBER'; 

v_sql := v_sql || ' LEFT JOIN REPORTS_MV."STAGE-ZIP2TERRITORY" Z2T' 
|| ' ON CG.ORGANIZATION_KEY = Z2T.ORGANIZATION_KEY'; 

v_sql := v_sql || ' ORDER BY A.GROUP_NUMBER, A.RECEIVED_DATE '; 
--dbms_output.put_line (v_sql); 
OPEN l_cursor FOR v_sql; END; /'

错误

  

PLS-00103(40:53):PLS-00103:在预期以下情况之一时遇到了符号“ [”:

     
      
  • &=-+; 在in是mod余数而不是rem
       <>或!=或〜=> = <= <>或like like2 like4
  •   

1 个答案:

答案 0 :(得分:1)

我当然不能说动态sql是否可以按书面形式执行,但是使用 alternative-quoting escapeing 应该可以编译它。

下面的示例使用q引号在字符串文字中允许单引号。 Oracle关于Literal的文档提供了有关该主题的更多信息。

CREATE OR REPLACE PROCEDURE REPORTS_MV.CUSTOM_REPORT(L_CURSOR OUT SYS_REFCURSOR)
AS
    V_SQL CLOB;
BEGIN
    V_SQL := Q'!SELECT
        REGEXP_SUBSTR(Z2T.DISTRICT_NUMBER, '[^-]+') AS "District Number",
        REGEXP_SUBSTR(Z2T.DISTRICT_NUMBER, '[^-]+', 1, 2) AS "District Name",
        REGEXP_SUBSTR(Z2T.TERRITORY_NUMBER, '[^-]+') AS "Territory Number",
        REGEXP_SUBSTR(Z2T.TERRITORY_NUMBER, '[^-]+', 1, 2) AS "Territory Name"
        FROM REPORTS_MV."STAGE-EMDEON_REDEMPTION" A
        LEFT JOIN REPORTS_MV."STAGE-CARD_GROUP" CG ON A.GROUP_NUMBER = CG.GROUP_NUMBER
        LEFT JOIN REPORTS_MV."STAGE-ZIP2TERRITORY" Z2T
        ON CG.ORGANIZATION_KEY = Z2T.ORGANIZATION_KEY
        ORDER BY A.GROUP_NUMBER, A.RECEIVED_DATE!';
    --DBMS_OUTPUT.PUT_LINE(V_SQL);
    OPEN L_CURSOR FOR V_SQL;
END;
/