开发者

Is Oracle's syntax diagram for PL/SQL blocks wrong?

开发者 https://www.devze.com 2023-03-30 15:36 出处:网络
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.

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.

  1. The declare section (above) consists of a curs开发者_JAVA百科or definition followed by a variable declaration (which in turn is an item declaration).

  2. An item declaration can only be an element of an item list 1.

  3. A cursor definition can only be an element of an item list 2.

  4. An item list 2 can never be followed by an item 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_block is effectively item_list_2 preceded by an optional item_list_1.

Further, cursor definitions (with the is bit) can only occur in item_list_2 and variable declarations (with or without an =) are part of the item_declaration set and can therefore only be in item_list_1.

Those facts make your code sample incorrect so, if it manages to compile, then either:

  • the syntax diagrams are wrong; or
  • the compiler doesn't follow them to the letter; or
  • your looking at code that's covered by different syntax diagrams.

On that last bullet point, interestingly enough, the syntax diagrams for 11.1 are slightly different.

The declare section can be item_list_1 or item_list_2 or item_list_1 followed by item_list_2.

Where it gets interesting is that item_list_1 can have any number of item_declaration entries and this includes both variable_declaration and cursor_declaration.

In 11.1, a cursor_declaration can be either a declaration or a definition, based on the language elements in 11.2 (in other words, there is no cursor_definition type 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 varchar and a NULL (a).


(a) I'll never pass up an opportunity to mention this and advance the cause of my beloved DB2 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号