Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, jignesh.

Asked: July 31, 2003 - 1:51 pm UTC

Last updated: January 14, 2013 - 12:50 pm UTC

Version: 8.1.7

Viewed 10K+ times! This question is

You Asked

Hi Tom,

Could you please explain the term "forward declaration" in packages. What exactly does it means?. when do we need it? ? ?



and Tom said...


it is unusual to use it, you use it when you have a procedure you want to code for whatever reason AFTER you reference it. you can "forward declare it"

say you have:

ops$tkyte@ORA920LAP> create or replace package demo_pkg
2 as
3 procedure p;
4 end;
5 /

Package created.

and in the body you have this:



ops$tkyte@ORA920LAP>
ops$tkyte@ORA920LAP> create or replace package body demo_pkg
2 as
3 procedure p
4 is
5 begin
6 p2;
7 end;
8
9 procedure p2
10 is
11 begin
12 null;
13 end;
14 end;
15 /

Warning: Package Body created with compilation errors.

ops$tkyte@ORA920LAP> show errors
Errors for PACKAGE BODY DEMO_PKG:

LINE/COL ERROR
-------- -----------------------------------------------------------------
6/3 PLS-00313: 'P2' not declared in this scope
6/3 PL/SQL: Statement ignored

that fails, p doesn't know about p2 yet, so you can:


ops$tkyte@ORA920LAP>
ops$tkyte@ORA920LAP> create or replace package body demo_pkg
2 as
3 procedure p2;

4
5 procedure p
6 is
7 begin
8 p2;
9 end;
10
11 procedure p2
12 is
13 begin
14 null;
15 end;
16 end;
17 /

Package body created.

forward declare p2, but it would be BETTER to just define p2 at the top of the package



Rating

  (11 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Great

A reader, August 01, 2003 - 8:48 am UTC

Great Answer

Very clear

jignesh, August 01, 2003 - 11:09 am UTC

Great answer with example asusual.

Thanks a LOT.

Great Sharing

Javed A. Khan, September 13, 2011 - 4:28 am UTC

Wonderfull explaination. Thanks Tom.

Wonderful Explanation

rakesh, May 03, 2012 - 3:47 am UTC

Explained and showed in wonderful way.

A reader, July 19, 2012 - 9:16 am UTC

yes great example, i was also facing same problem but not it is working. Thanks

Good explanation

Catherine, October 04, 2012 - 12:22 am UTC

Thank u so much. i tried it and it's working fine. the explanation was clear cut.

required forward declaration

Naeel Maqsudov, January 09, 2013 - 7:39 am UTC

declare
procedure qwe1;
-- REQUIRED forward declaration
-- in case of mutual recursion
procedure qwe2
as
begin
...
qwe1;
end;

procedure qwe1
as
begin
...
qwe2;
end;

begin
null;
end;
/

Here we cannot avoid of using a forward declaration.
Of course this is a very rare case.
Rather, just methodical.
Tom Kyte
January 14, 2013 - 12:50 pm UTC

some would say "a very bad case"

to have A call B and B call A would fit my description of "something you should probably not be doing"

One more situation when the forward reclaration is mandatory

Naeel Maqsudov, January 13, 2013 - 1:50 pm UTC

Declaration section must contain local function declarations after variables declaration. If you want to initialize a variable by a function, you have to use forward declaration of the function:

declare
function TVar1 return anytype;

x anydata;
Var1 anytype:=TVar1;
...
function TVar1 return anytype as
AType anytype;
begin
.....
RETURN AType;
end;
...
BEGIN
...
END;
/

pl/sql

Natarajan, September 22, 2015 - 8:51 am UTC

Vow! Very Nice and clear explanation,a lot of Thanks

Vikrant, April 14, 2016 - 7:23 am UTC

nice explanation thanks thomas.

Great Explanation!!!

Esakki Raja Esakki Muthu, May 04, 2017 - 1:16 pm UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library