Is this a bug in the PL/SQL Compiler?
In our SVN-Code Repository, I came across a package specification that -after removing a few lines- boils down to
create or replace package tq84 as
return varchar2(10);
end tq84;
/
It seems to me that such a specification doesn't make lot of sense and therefore should not compile at all. But maybe, I don't see the obvious, so: is this really a bug?
For completness' sake:
me @ xxx.yyy.zz > select * from v$version;
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Productio
NLSRTL Version 10.2.0.4.0 - Production
Edit: it has been suggested that in specification as given above return
is not the keyword but a (package-)variable. This seems not to be the case however, since the following compiles equally fine:
create or replace package tq84 as
return varchar2(10);
return number;
return date;
end tq84;
/
and clearly, the compiler should tell me that I declare the same variable multiple times.
EDIT 2: JOTN is right, of course, and return
IS a variable, and furthermore, the
compiler doesn't tell upfront, if a varia开发者_如何学Cble with the same name is declared twice or more, instead, it's the runtime environment, that does.
So, with that in mind, it's possible to compile something like
create or replace package return as
subtype return is varchar2(10);
end return;
/
create or replace package tq84 as
constant constant
return . return := 'return';
function function
return return . return;
end tq84;
/
which looks strange, at least at first sight.
So then, I guess, it's not a compiler bug because return
is allowed as a variable name, but then, it's disputable if the compiler should at least give a warning if a variable with the same name is declared multiple times.
Apparently it allows you to use the name "return" as a variable. In that case it's declaring a package variable. I would have figured that would fail because it's a keyword, but I tried it and it worked.
Try this code:
create or replace package tq84 as
return varchar2(10);
somevar varchar2(5);
somevar varchar2(5);
end tq84;
/
set serveroutput on
BEGIN
tq84.return:='Test';
dbms_output.put_line(tq84.return);
END;
/
That shows return as a variable and it allows the same variable of another name to be declared more than once.
Now if you try to access somevar, then you get this:
PLS-00371: at most one declaration for 'TQ84.SOMEVAR' is permitted
So apparently it delays that check for some reason.
I just found out how you can detect these problems at compile time. Add this:
alter session set plsql_warnings = 'enable:all';
That code above compiles with these warnings:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/1 PLW-05018: unit TQ84 omitted optional AUTHID clause; default
value DEFINER used
2/3 PLW-06010: keyword "RETURN" used as a defined name
4/3 PLW-05001: previous use of 'SOMEVAR' (at line 3) conflicts with
this use
Just wanted to add further illustration that JOTN's answer is correct. It does seem bad that the compiler lets you declare multiple variables with the same name, but maybe this is a side effect of how PL/SQL implements method overloading. At runtime, it does raise an error if the name has been declared multiple times.
In a sample like your original example, it is clear that return
is being used as a variable name. You can assign and read its value as shown below.
dev> set serveroutput on
dev> create or replace package test
2 as
3 return varchar2(10);
4 end test;
5 /
Package created.
dev> exec test.return := 'Hi!';
PL/SQL procedure successfully completed.
dev> exec dbms_output.put_line( test.return );
Hi!
PL/SQL procedure successfully completed.
dev> create or replace package test
2 as
3 return varchar2(10);
4 return varchar2(20);
5 end test;
6 /
Package created.
dev> exec test.return := 'Hi!';
BEGIN test.return := 'Hi!'; END;
*
ERROR at line 1:
ORA-06550: line 1, column 12:
PLS-00371: at most one declaration for 'TEST.RETURN' is permitted
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
精彩评论