Monday 18 September 2017

PACKAGE in Oracle


  • A package is a schema object that groups logically related PL/SQL types, variables, constants, procedures, subprograms, cursors, and exceptions.
  • A package is compiled and stored in the database, where many applications can share its contents. You can think of a package as an application.
  • Every cursor or subprogram declaration in the package specification must have a corresponding definition in the package body.
  • The headings of corresponding subprogram declarations and definitions must match word for word, except for white space.


  • The cursors and subprograms declared in the package specification and defined in the package body are public items that can be referenced from outside the package.
  • The package body can also declare and define private items that cannot be referenced from outside the package, but are necessary for the internal workings of the package.
  • The body can have an initialization part, whose statements initialize public variables and do other one-time setup steps.
  • The initialization part runs only the first time the package is referenced. The initialization part can include an exception handler.

Package Instantiation and Initialization:

  • When a session references a package item, Oracle Database instantiates the package for that session. Every session that references a package has its own instantiation of that package.
  • When Oracle Database instantiates a package, it initializes it. Initialization includes whichever of the following are applicable:
    - Assigning initial values to public constants.
    - Assigning initial values to public variables whose declarations specify them.
    - Executing the initialization part of the package body
Advantages of Packages:

  • Cohesion: all the procedures and functions relating to a specfic sub-system are in one program unit. This is just good design practice but it's also easier to manage, e.g. in source control.
  • Constants, sub-types and other useful things: Anything we can define in a package spec can be shared with other programs, for instance user-defined exceptions.
  • Overloading: the ability to define a procedure or function with the same name but different signatures.
  • Security: defining private procedures in the package body which can only be used by the package because they aren't exposed in the specification.
  • Sharing common code: another benefit of private procedures.
  • We only need to grant EXECUTE on a package rather than on several procedures.

No comments:

Post a Comment