在PL-SQL中找不到预期的From关键字

时间:2019-03-29 13:34:27

标签: oracle plsql

我有以下代码:

v_SQL := '
-- ==========================================================
--  Main Unpivot Table -  To move years from column headings 
--  to rows values
-- ==========================================================

SELECT 
CAST(FieldYear AS Number) FieldYear
, FieldType
, CASE FieldValue 
WHEN '''' THEN NULL
ELSE CAST (FieldValue AS Number(38,8))
END FieldValue
FROM 
(SELECT ' || 
v_InsertType ||' AS FieldType
,Y1 as ' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '
,Y2 as ' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '
,Y3 as ' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '
,Y4 as ' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '
,Y5 as ' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '
,Y6 as ' || CAST(v_FY0     - v_OffSet AS CHAR) || ' 
,Y7 as ' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '
,Y8 as ' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '
,Y9 as ' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '
,Y10 as ' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '
,Y11 as ' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '
,Y12 as ' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '
,Y13 as ' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '
,Y14 as ' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '
,Y15 as ' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '
,Y16 as ' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '
,Y17 as ' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '
,Y18 as ' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '
,Y19 as ' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '
,Y20 as ' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '
,Y21 as ' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '
,Y22 as ' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '
,Y23 as ' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '
,Y24 as ' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '
,Y25 as ' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '
,Y26 as ' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '
,Y27 as ' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '
,Y28 as ' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '
,Y29 as ' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '
,Y30 as ' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '
,Y31 as ' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '
,Y32 as ' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '
,Y33 as ' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '
,Y34 as ' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '
,Y35 as ' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '

FROM ' || 
v_tblInsertType || ') UP
UNPIVOT
(FieldValue FOR FieldYear IN 
(' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0     - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '
,' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '
)
)AS unpvt

Execute immediate v_SQL;

其中v_FY0为NUMBER(10); v_SQL NVarchar2(4000),v_InsertType VARCHAR2(4000);和v_OffSet Number(10); v_tblInsertType VARCHAR2(4000);

执行后,我得到:v_FY0 = 2018和v_OffSet = 0;

执行后,出现错误-ORA-00923:在期望的位置找不到FROM关键字。

如何解决此问题以及如何使其正常工作。

2 个答案:

答案 0 :(得分:2)

您不能在as子句中使用from

此查询:

select * from (select dummy from dual) as tab

产生错误ORA-00933。因此,只需删除as

select * from (select dummy from dual) tab

答案 1 :(得分:0)

以下是修改后的代码,可为我提供所需的答案:

v_SQL := '
-- ==========================================================
--  Main Unpivot Table -  To move years from column headings 
--  to rows values
-- ==========================================================

SELECT 
     CAST(FieldYear AS Number) FieldYear
    , FieldType
    , CASE FieldValue 
        WHEN n'''' THEN NULL
        ELSE CAST (FieldValue AS Number(38,8))
      END FieldValue
FROM 
   (SELECT ' || 
    v_InsertType ||' AS FieldType
    ,Y1 as "' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '"
    ,Y2 as "' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '"
    ,Y3 as "' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '"
    ,Y4 as "' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '"
    ,Y5 as "' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '"
    ,Y6 as "' || CAST(v_FY0     - v_OffSet AS CHAR) || '"
    ,Y7 as "' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '"
    ,Y8 as "' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '"
    ,Y9 as "' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '"
    ,Y10 as "' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '"
    ,Y11 as "' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '"
    ,Y12 as "' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '"
    ,Y13 as "' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '"
    ,Y14 as "' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '"
    ,Y15 as "' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '"
    ,Y16 as "' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '"
    ,Y17 as "' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '"
    ,Y18 as "' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '"
    ,Y19 as "' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '"
    ,Y20 as "' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '"
    ,Y21 as "' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '"
    ,Y22 as "' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '"
    ,Y23 as  "' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '"
    ,Y24 as "' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '"
    ,Y25 as "' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '"
    ,Y26 as "' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '"
    ,Y27 as "' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '"
    ,Y28 as "' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '"
    ,Y29 as "' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '"
    ,Y30 as "' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '"
    ,Y31 as "' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '"
    ,Y32 as "' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '"
    ,Y33 as "' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '"
    ,Y34 as "' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '"
    ,Y35 as "' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '"

  FROM ' || 
        v_tblInsertType || ') UP
UNPIVOT
   (FieldValue FOR FieldYear IN 
    ("' || CAST(v_FY0 - 5 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 - 4 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 - 3 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 - 2 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 - 1 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0     - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 1 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 2 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 3 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 4 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 5 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 6 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 7 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 8 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 9 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 10 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 11 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 12 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 13 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 14 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 15 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 16 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 17 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 18 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 19 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 20 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 21 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 22 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 23 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 24 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 25 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 26 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 27 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 28 - v_OffSet AS CHAR) || '"
    ,"' || CAST(v_FY0 + 29 - v_OffSet AS CHAR) || '"
)
) unpvt' ;

Execute immediate v_SQL;