I suspect that the syntax diagram for a plsql_block as given in the
Oracle® Database PL/SQL Language Reference for Relese 2 is wrong.
(For reference, here’s the current link to that document)
The following piece of PL/SQL compiles fine:
declare
cursor
cursor_definition
is select * from dual;
variable_declaration number;
begin
null;
end;
The following statements are assumptions that I make based on the piece of PL/SQL above and based on Oracle’s syntax diagram.
-
The declare section (above) consists of a
cursor definitionfollowed by avariable declaration(which in turn is anitem declaration). -
An
item declarationcan only be an element of anitem list 1. -
A
cursor definitioncan only be an element of anitem list 2. -
An
item list 2can never be followed by anitem list 1.
Now, the variable declaration following the cursor definition contradicts point 4. Therefore I conclude that the
syntax diagram is wrong.
Maybe I am overlooking something, in which case I’d be very grateful for pointing this out to me.
Please understand that a wrong syntax diagram per se is no big deal to me. But I am in the process of writing a PL/SQL parser
and the parser stumbles for the exact situation given with the example PL/SQL code. So, in order to improve the parser, I’d like
to have a more authorative sequence diagram.
I concur. The syntax diagrams explicitly state that a
plsql_blockis effectivelyitem_list_2preceded by an optionalitem_list_1.Further, cursor definitions (with the
isbit) can only occur initem_list_2and variable declarations (with or without an=) are part of theitem_declarationset and can therefore only be initem_list_1.Those facts make your code sample incorrect so, if it manages to compile, then either:
On that last bullet point, interestingly enough, the syntax diagrams for 11.1 are slightly different.
The declare section can be
item_list_1oritem_list_2oritem_list_1followed byitem_list_2.Where it gets interesting is that
item_list_1can have any number ofitem_declarationentries and this includes bothvariable_declarationandcursor_declaration.In 11.1, a
cursor_declarationcan be either a declaration or a definition, based on the language elements in 11.2 (in other words, there is nocursor_definitiontype since the declaration allows both in the declaration).So what you have is perfectly valid in 11.1 so the first thing I’d check is that you’re actually running 11.2 where that successful compilation is taking place.
It’s still possible of course that you’re running 11.2 and the syntax diagrams are wrong, in which case you should complain bitterly to Oracle but I don’t know what sort of a response you’ll get from a company whose flagship database product can’t tell the difference between an empty
varcharand a NULL (a).(a) I’ll never pass up an opportunity to mention this and advance the cause of my beloved DB2 🙂