 |
|
EnterpriseDB: Declaring and Calling Packaged Code
Oracle Tips by
Burleson
|
A
package is best thought of as a library of code. It's like a .jar
file in Java or a .lib in many other languages. It is a set of
procedures, functions and variables stored together in a single unit.
While
there is nothing magical about the package, there are some special
features of SPL that you can only use when you take advantage of
packages.
*
Encapsulation - Packages provide a separation between the public
definition of a procedure or function and the actual implementation of
that procedure or function
*
Performance – When you call a single procedure or function in a
package, the entire package is loaded into memory allowing faster
response of the other related code
*
Organization – Packages allow you to group related information
together in a single unit
*
Features – Packages allow you to have public and protected variables
in addition to local variables and packages allow you to overload
procedures and functions
Package Specification
The
package specification defines the public view of your package. In the
specification, you will define any public variables and types and any
public procedures and functions.
The
package specification does not contain any executable code or
exception handling. The specification contains only the definitions
of the items used in the public interface. The syntax for a package
specification is:
CREATE OR REPLACE PACKAGE <package name>
AS
[public type declaration]
[public variable declaration]
[public constant declaration]
[public cursor declaration]
[PROCEDURE <procedure name>[(<variable list>)];]
[FUNCTION <function name>[(<variable list>)]
RETURN <data type>;]
END;
A
package specification is required to contain at least one definition
of some sort but is not required to contain any specification
definition. That means that a specification may only contain a single
variable definition. If a specification does not contain a procedure
or function definition, it will not have a package body.
Package Body
Package
bodies follow the same block structure as anonymous blocks, procedures
and functions. A package body is NOT required for a specification.
The
package is where the executable code and exception handling for a
package resides. In addition to the public declarations, the package
body may contain private functions and procedures that are not
callable outside the package. The syntax for a package body is:
CREATE OR REPLACE PACKAGE BODY <package name>
AS
[private type declaration]
[private variable declaration]
[private constant declaration]
[private cursor declaration]
[PROCEDURE <procedure name>[(<variable list>)]
AS
<declarations>;
BEGIN
<executable and exception handling>;
END;]
[FUNCTION <function name>[(<variable list>)]
RETURN <data type>
AS
<declarations>;
BEGIN
<executable and exception handling>;
RETURN <return value>;
END;]
[BEGIN
<package initialization code>]
END;
In
addition to declaring private variables and providing the code for the
procedures and functions, a package body contains an initialization
section. The initialization section can initialize any public or
private variables and can perform any additional functionality that
would normally be performed in a procedure, including running SQL
commands. The initialization section must contain at least one
executable statement.
The
procedures and functions in a package body are defined like a
standalone procedure or function and perform in the same way.
Parameters are available and all three modes (IN, OUT and IN OUT) are
allowed.
This
is an excerpt from the book "EnterpriseDB:
The Definitive Reference" by Rampant TechPress. |