从关键字在 PL-SQL 中未找到预期的位置

Posted

技术标签:

【中文标题】从关键字在 PL-SQL 中未找到预期的位置【英文标题】:From keyword not found where expected in PL-SQL 【发布时间】:2019-03-29 13:34:27 【问题描述】:

我有以下代码:

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 编号(10); v_tblInsertType VARCHAR2(4000);

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

执行时,我收到错误 - ORA-00923: FROM Keyword not found where expected.

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

【问题讨论】:

您为什么不尝试通过 dbms_output.put_line 输出v_sql 变量的结果 - 然后您可以看到创建的查询是什么,您应该能够更多地发现问题很容易。 请尝试将) AS unpvt 更改为) unpvt。删除AS @PonderStibbons - 这实际上是答案(而不仅仅是评论)。不妨将其发布为答案。 顺便说一句,Execute immediate v_SQL; 对选择查询没有用处,除非您将结果存储在某处。它不会返回任何结果。如果您使用的是 12c 及更高版本,DBMS_SQL.RETURN_RESULT 可能对动态光标有用。 【参考方案1】:

您不能在from 子句中使用as

这个查询:

select * from (select dummy from dual) as tab

产生错误ORA-00933。所以只需删除as

select * from (select dummy from dual) tab

【讨论】:

【参考方案2】:

这是给我所需答案的修改后的代码:

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;

【讨论】:

以上是关于从关键字在 PL-SQL 中未找到预期的位置的主要内容,如果未能解决你的问题,请参考以下文章

错误 - 未在预期位置找到关键字

在 SQL 中选择 SEQUENCE:FROM 关键字未在预期的位置找到

我将如何修复这些“ORA-00933:SQL 命令未正确结束”“ORA-00923:未在预期位置找到 FROM 关键字”错误?

RESTKit 异常:在搜索的关键路径中未找到可映射的对象表示

listagg DB链接问题

从关键字定义,但我仍然得到错误