0% found this document useful (0 votes)
21 views12 pages

PLSQL Packages

Oracle Packages are unique database objects that group related PL/SQL types, variables, constants, subprograms, cursors, and exceptions into a single definition, consisting of a mandatory specification and an optional body. They enhance modularity, application design, and performance by encapsulating implementation details and allowing for shared public variables across sessions. The package state persists for the session's duration, and guidelines suggest designing specifications before bodies and keeping packages general for future reuse.

Uploaded by

ssuresh2107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

PLSQL Packages

Oracle Packages are unique database objects that group related PL/SQL types, variables, constants, subprograms, cursors, and exceptions into a single definition, consisting of a mandatory specification and an optional body. They enhance modularity, application design, and performance by encapsulating implementation details and allowing for shared public variables across sessions. The package state persists for the session's duration, and guidelines suggest designing specifications before bodies and keeping packages general for future reuse.

Uploaded by

ssuresh2107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PL/SQL – Packages

• Oracle Packages are database objects that are unique to the Oracle DBMS.
• They are containers that group logically-related objects into a single definition
• Packages have two parts - a specification and a body.
• A package is a schema object that groups logically related PL/SQL types, variables, constants,
subprograms, cursors, and exceptions.
• A package is compiled and stored in the database
• A package specification is mandatory while the package body can be required or optional
• A package always has a specification, which declares the public items that can be referenced from outside
the package
• If the public items include cursors or subprograms, then the package must also have a body .
• The body can also declare and define private items that cannot be referenced from outside the package
PL/SQL – Packages
PL/SQL – Contents of Packages
PL/SQL – Why Packages

Modularity
Packages let you encapsulate logically related types, variables, constants, subprograms, cursors, and exceptions in named PL/SQL modules
Easier Application Design
When designing an application, all you need initially is the interface information in the package specifications. You can code and compile
specifications without their bodies. Next, you can compile standalone subprograms that reference the packages.
Hidden Implementation Details
Packages let you share your interface information in the package specification, and hide the implementation details in the package body.
Hiding the implementation details in the body has these advantages:
•You can change the implementation details without affecting the application interface.
•Application users cannot develop code that depends on implementation details that you might want
Added Functionality
Package public variables and cursors can persist for the life of a session. They can be shared by all subprograms that run in the environment.
Better Performance
The first time you invoke a package subprogram, Oracle Database loads the whole package into memory. Subsequent invocations of other
subprograms in same the package require no disk I/O.
Easier to Grant Roles
You can grant roles on the package, instead of granting roles on each object in the package
PL/SQL – Packages

Package Specification
• A package specification declares public items. The scope of a public item is the schema of the package. A public item is
visible everywhere in the schema
Appropriate public items are:
• Types, variables, constants, subprograms, cursors, and exceptions
• Associative array types of standalone subprogram parameters(You cannot declare an associative array type at schema
level.)
• Variables that must remain available between subprogram invocations in the same session
• Subprograms that read and write public variables ("get" and "set" subprograms)
• Subprograms that invoke each other
• Overloaded subprograms
PL/SQL – Packages
PL/SQL – Packages

Package Body
• If a package specification declares cursors or subprograms, then a package body is required; otherwise, it is optional
• 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
• The package body can also declare and define private items that cannot be referenced from outside the package
• the body can have an initialization part, whose statements initialize public variables and do other one-time setup steps.
• You can change the package body without changing the specification or the references to the public items
PL/SQL – Packages
PL/SQL – Packages

Package Instantiation
• 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
• Assigning initial values to public constants
• Assigning initial values to public variables whose declarations specify them
• Executing the initialization part of the package body
• The values of the variables, constants, and cursors that a package declares (in either its specification or body) comprise
its package state
• If a PL/SQL package declares at least one variable, constant, or cursor, then the package is stateful; otherwise, it is
stateless
• The package state persists for the life of a session, except in these situations:
• The package is SERIALLY_REUSABLE
• Any of the session's instantiated packages are invalidated and revalidated
PL/SQL – Packages

Package Writing Guidelines


• Become familiar with the packages that Oracle Database supplies, and avoid writing packages that duplicate their
features.
• Keep your packages general so that future applications can reuse them
• Design and define the package specifications before the package bodies
• In package specifications, declare only items that must be visible to invoking programs
• Declare public cursors in package specifications and define them in package bodies
• Assign initial values in the initialization part of the package body instead of in declarations
This practice has these advantages:
• The code for computing the initial values can be more complex and better documented.
• If computing an initial value raises an exception, the initialization part can handle it with its own exception handler
PL/SQL – Packages
PL/SQL – Packages

Standard
• A package named STANDARD defines the PL/SQL environment.
• The package specification declares public types, variables, exceptions, subprograms, which are available automatically to PL/SQL
programs
• For example, package STANDARD declares function ABS, which returns the absolute value of its argument
• The contents of package STANDARD are directly visible to applications. You need not qualify references to its contents by prefixing the
package name.
• For example, you might invoke ABS from a database trigger, stored subprogram, Oracle tool, or 3GL application
• If you declare your own version of ABS, your local declaration overrides the public declaration. You can still invoke the SQL function by
specifying its full name
abs_diff := STANDARD.ABS(x - y);
• Most SQL functions are overloaded. For example, package STANDARD contains these declarations:

You might also like