【问题标题】:Order of declaration in an anonymous pl/sql block匿名 pl/sql 块中的声明顺序
【发布时间】:2010-06-09 14:54:27
【问题描述】:

我有一个匿名 pl/sql 块,其中声明了一个过程以及一个游标。如果我在游标之前声明该过程,它将失败。是否要求在过程之前声明游标?

pl/sql 块中的声明顺序还有哪些其他规则?

这行得通:

DECLARE
 cursor cur is select 1 from dual;
 procedure foo as begin null; end foo;
BEGIN
 null;
END;

这会失败并出现错误PLS-00103: Encountered the symbol "CURSOR" when expecting one of the following: begin function package pragma procedure form

DECLARE
 procedure foo as begin null; end foo;
 cursor cur is select 1 from dual;
BEGIN
 null;
END;

【问题讨论】:

    标签: oracle plsql


    【解决方案1】:

    游标、变量、常量和类型需要在包/函数之前声明。

    这个也会失败:

    DECLARE
     procedure foo as begin null; end foo;
     x VARCHAR2(10);
    BEGIN
     null;
    END;
    

    【讨论】:

    • 文档参考在这里download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/…不是很清楚,但是“项目声明”(例如变量)在列表1中,并且必须在“过程/函数定义”之前清单 2.
    • 修改代码时很容易错过!我在游标之前声明了一个函数,我得到了这个:ORA-06550:第 XXX 行,第 X 列:PLS-00103:在期望以下之一时遇到符号“CURSOR”:开始函数杂注过程
    【解决方案2】:

    如果你想声明一个对子过程也可用的游标,只需添加另一个匿名块:

    DECLARE
     cursor cur is select 1 from dual;
    BEGIN
     DECLARE
      procedure foo as begin null; end foo;
     BEGIN
      null;
     END;
    END;
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 2017-09-02
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多