-2

从 PL/SQL 调用此查询时出现 ORA-06502 错误。但是,如果我从 SQL 提示符尝试它正在工作。

在 SQL 提示符下,这给出了预期的输出:

 SELECT *
  FROM
  (
   SELECT
       COL.BAN,
       MAX (COL.COL_ACTV_CODE) AS COL_ACTV_CODE,
       MAX (TO_CHAR(COL.COL_ACTV_DATE,'MM')) AS COL_ACTV_DATE
   FROM
       COLLECTION               COL,
       COLLECTION_ACTIVITIES    CAC
   WHERE (CAC.SEVERITY_LEVEL , TO_CHAR(COL.COL_ACTV_DATE,'YYYYMM')) IN
        (SELECT
               MAX(CAC.SEVERITY_LEVEL),
               MAX(TO_CHAR(COL.COL_ACTV_DATE, 'YYYYMM'))
          FROM
               COLLECTION               COL,
               COLLECTION_ACTIVITIES    CAC
          WHERE
               COL.COL_ACTV_CODE    = CAC.COL_ACTIVITY_CODE
          GROUP BY  TO_CHAR (COL.COL_ACTV_DATE , 'YYYYMM')
         )
    GROUP BY  COL.BAN
    ORDER BY  TO_CHAR (COL.COL_ACTV_DATE , 'YYYYMM') DESC
     )
     PIVOT
    (
       MAX( COL_ACTV_CODE)
     FOR COL_ACTV_DATE in ('01' as "JAN", '02' as "FEB", '03' as "MAR"));

       BAN J F M A M J J A S O N D
---------- - - - - - - - - - - - -
  90314228                 W
  90314009     K
  90314748                     E
  90314568                   E
  90314328                 W

但是从 PL/SQL:

BEGIN

STMT_STR := 'CREATE TABLE ACD_COL_ST
            PCTUSED 90
            NOLOGGING
            AS
 SELECT *
  FROM
  (
   SELECT
       COL.BAN,
       MAX (COL.COL_ACTV_CODE) AS COL_ACTV_CODE,
       MAX (TO_CHAR(COL.COL_ACTV_DATE,'MM')) AS COL_ACTV_DATE
   FROM
       COLLECTION               COL,
       COLLECTION_ACTIVITIES    CAC
   WHERE (CAC.SEVERITY_LEVEL , TO_CHAR(COL.COL_ACTV_DATE,'YYYYMM')) IN
        (SELECT
               MAX(CAC.SEVERITY_LEVEL),
               MAX(TO_CHAR(COL.COL_ACTV_DATE, 'YYYYMM'))
          FROM
               COLLECTION               COL,
               COLLECTION_ACTIVITIES    CAC
          WHERE
               COL.COL_ACTV_CODE    = CAC.COL_ACTIVITY_CODE
          GROUP BY  TO_CHAR (COL.COL_ACTV_DATE , 'YYYYMM')
         )
    GROUP BY  COL.BAN
    ORDER BY  TO_CHAR (COL.COL_ACTV_DATE , 'YYYYMM') DESC
     )
     PIVOT
    (
       MAX( COL_ACTV_CODE)
     FOR COL_ACTV_DATE in ('01' as "JAN", '02' as "FEB", '03' as "MAR"))';

EXECUTE IMMEDIATE STMT_STR;

EXCEPTION WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error was encountered creating table ACD_COL_ST'||SQLCODE||'  - ERROR - '||SQLERRM);
END;
COMMIT;

我收到一个错误:

ORA-06502: PL/SQL: 数字或值错误: 字符串缓冲区太小

4

1 回答 1

0

我只是检查代码。在我们回答之前,请您告诉我为什么要使用动态 SQL 在运行时创建表。不可能先创建表,然后使用比 PLSQL 更简单的 SQL 插入表。无论如何,我在你的代码中看到只有字符串有单引号。我们需要在动态查询中使用 '' for '。请参阅下面的查询,我现在没有工作区,因此无法验证查询。请原谅任何语法错误。

DECLARE
STMT_STR LONG;
BEGIN
STMT_STR := 
        'CREATE TABLE ACD_COL_ST                        
        AS 
        SELECT *  
        FROM  
        (   
        SELECT       
        COL.BAN,       
        MAX (COL.COL_ACTV_CODE) AS COL_ACTV_CODE,       
        MAX (TO_CHAR(COL.COL_ACTV_DATE,''MM'')) AS COL_ACTV_DATE   
        FROM       
        COLLECTION               COL,       
        COLLECTION_ACTIVITIES    CAC   
        WHERE (CAC.SEVERITY_LEVEL , TO_CHAR(COL.COL_ACTV_DATE,''YYYYMM'')) IN        
        (SELECT               
        MAX(CAC.SEVERITY_LEVEL),               
        MAX(TO_CHAR(COL.COL_ACTV_DATE, ''YYYYMM''))          
        FROM               
        COLLECTION               COL,               
        COLLECTION_ACTIVITIES    CAC          
        WHERE               
        COL.COL_ACTV_CODE    = CAC.COL_ACTIVITY_CODE          
        GROUP BY  TO_CHAR (COL.COL_ACTV_DATE , ''YYYYMM'')         
        )    
        GROUP BY  COL.BAN    
        ORDER BY  TO_CHAR (COL.COL_ACTV_DATE , ''YYYYMM'') DESC     
        )     
        PIVOT    
        (       
        MAX( COL_ACTV_CODE)     
        FOR COL_ACTV_DATE in (''01'' as JAN, ''02'' as FEB, ''03'' as MAR))';
  EXECUTE IMMEDIATE STMT_STR;
EXCEPTION
WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE('An error was encountered creating table ACD_COL_ST'||SQLCODE||'  - ERROR - '||SQLERRM);
END;
于 2016-03-22T12:08:05.973 回答