2010-06-09 55 views
5

我有一个匿名的pl/sql块,其中声明了一个过程以及一个游标。如果我在游标之前声明过程,它会失败。是否有要求在程序之前声明游标?声明在匿名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; 

回答

12

光标,变量,常量和类型需要前包/函数声明。如果你想声明的游标提供给子过程以及

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

文档引用这里 http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28370/block.htm#i32791 它不是很清楚,但“项目声明”(如变量)在列表中1并且必须在“程序/函数定义”之前出现ich在列表2中。 – 2010-06-09 23:17:06

+0

@Gary:非常好,谢谢! – 2010-06-10 14:51:50

0

,只需添加另一个匿名块:

这一次会失败太

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