Re: PL/SQL function call syntax hassle

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: 1997/10/17
Message-ID: <344973b8.5803595_at_newshost>#1/1


I think you have that backwards, you must declare variables BEFORE objects in pl/sql. Consider the following:

SQL> declare

  2     x int;
  3     procedure foo is begin null; end;
  4  begin
  5     null;

  6 end;
  7 /  

PL/SQL procedure successfully completed.

  • The above worked since x the integer was defined before foo the procedure. thats the documented syntax for pl/sql -- variables and then objects.

SQL> declare

  2     procedure foo is begin null; end;
  3     x int;
  4  begin
  5     null;

  6 end;
  7 /
   x int;
   *
ERROR at line 3:
ORA-06550: line 3, column 4:
PLS-00103: Encountered the symbol "X" when expecting one of the following: begin function package pragma procedure form  
  • the above does not work since the procedure foo precedes the variable declaration X in the same declare block. thats invalid syntax.... so how to put FOO in front of X? 2 ways:

SQL> declare

  2     procedure foo;
  3     x int;
  4     procedure foo is begin null; end;
  5  begin
  6     null;

  7 end;
  8 /  

PL/SQL procedure successfully completed.

  • Use a forward declare. foo is defined before X. why might this be useful? consider this block: declare function foo return number; x int default FOO; function foo return number is begin return 5; end; begin dbms_output.put_line(x); end;

that'll work. I can forward declare the function and refer to it in initializations, implementing it after the variable declare.  

SQL> declare
  2 procedure foo is begin null; end;   3 begin

  4     declare
  5                  x int;
  6     begin
  7              null;
  8     end;

  9 end;
 10 /  

PL/SQL procedure successfully completed.  

  • Use a nested declare block. Similar to the forward declare but the scoping of X is subtly different (foo has a larger scope then X)....

On Sat, 18 Oct 1997 01:59:01 GMT, ua.ude.ug_at_doowtsae.c (Chris Eastwood) wrote:

>HiYa
>
>when tryingto use a procedure in a PL/SQL script I have found that I
>have to declare the local variables BEFORE I declare the PROCEDURES.
>
>Like this works:
>
>DECLARE
>
> PROCEDURE Boing(p_in NUMBER) IS
> -- declare local variables
> cart NUMBER(3);
> float NUMBER(3);
> BEGIN
> cart := 2;
> float := p_in * cart;
> END Boing;
>
> FUNCTION Skew(p_in NUMBER) RETURN NUMBER IS
> -- declare local variables
> one NUMBER(3);
> two NUMBER(3);
> BEGIN
> one := 1;
> two := p_in * one;
> RETURN two;
> END Skew;
>
> -- local variables
> startNumber NUMBER;
> displayNumber NUMBER;
> resultNumber NUMBER;
>
>
>
>BEGIN
> dbms_output.enable(1000000);
> startNumber := 5;
> Boing(startNumber);
>
>blah blah blah....
>
>and this doesn't:
>DECLARE
>
> -- local variables
> startNumber NUMBER;
> displayNumber NUMBER;
> resultNumber NUMBER;
>
>
> PROCEDURE Boing(p_in NUMBER) IS
> -- declare local variables
> cart NUMBER(3);
> float NUMBER(3);
> BEGIN
> cart := 2;
> float := p_in * cart;
> END Boing;
>
> FUNCTION Skew(p_in NUMBER) RETURN NUMBER IS
> -- declare local variables
> one NUMBER(3);
> two NUMBER(3);
> BEGIN
> one := 1;
> two := p_in * one;
> RETURN two;
> END Skew;
>
>BEGIN
> dbms_output.enable(1000000);
> startNumber := 5;
> Boing(startNumber);
>
>blah blah blah....
>
>why?
>
>Thanks
>
>---
>
>Chris Eastwood email the mirror image above
>Software Engineer ie-> doowtsae | eastwood
>ITS its a pain I know, but I think it'll
>Griffith University fool the robot mailers (I hope;-)
>Queensland if its all too hard, try the most
>AUSTRALIA obvious address ;-)
>
>************************************************************************
>
>'But oh, beamish nephew, beware of the day,
> If your Snark be a Boojum! For then
>You will softly and suddenly vanish away,
> And never be met with again!'

Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Bethesda MD

http://govt.us.oracle.com/ -- downloadable utilities



Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Fri Oct 17 1997 - 00:00:00 CEST

Original text of this message