Skip to Main Content
  • Questions
  • Can we create nested package in PL/SQL like we do in Java?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Susanto.

Asked: January 16, 2018 - 7:35 am UTC

Last updated: January 16, 2018 - 10:20 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Can we create nested package in PL/SQL like we do in Java? I mean can we create a package inside package in Oracle PL/SQL 11g?

and Chris said...

Nope:

create or replace package pkg as 
  package pkg_nested as 
    var integer;
  end;
end;
/
sho err

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/22     PLS-00103: Encountered the symbol "AS" when expecting one of the following:
         := . ( @ % ; not null range default character
5/1      PLS-00103: Encountered the symbol "END"

create or replace package pkg as 
  var int;
end;
/
sho err

No errors.

create or replace package body pkg as 
  package pkg_nested as 
    var integer;
  end;
end;
/
sho err

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/22     PLS-00103: Encountered the symbol "AS" when expecting one of the following:
         := . ( @ % ; not null range default character
5/1      PLS-00103: Encountered the symbol "END"


But you can nest procedures and functions within each other:

create or replace procedure p as
  procedure p_nest as
    procedure p_nest2 as
      procedure p_nest3 as
      begin
        dbms_output.put_line('Nested');
      end p_nest3;
    begin
      p_nest3();
    end p_nest2;
  begin
    p_nest2();
  end p_nest;

begin
  p_nest();
end p;
/

exec p();

Nested


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

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