The specification defines how a developer can use the package:
which programs can be called,
what cursors can be opened.
The body contains the implementation of the programs (and, perhaps, cursors) listed in the specification, plus other code elements as needed.
Suppose that I need to write code to retrieve the full name of an employee whose name is in the form "last, first".
That seems easy enough to write:
PROCEDURE process_employee (
employee_id_in IN employees.employee_id%TYPE)
SELECT last_name || ',' || first_name
WHERE employee_id = employee_id_in;
Yet there are many problems lurking in this seemingly transparent code:
I have hardcoded the length of the l_fullname variable. I did this because it is a derived value, the concatenation of two column values.
I did not, therefore, have a column against which I could %TYPE the declaration. This could cause difficulties over time if the size of last_name and/or first_name columns are expanded.
I have also hardcoded or explicitly placed in this block the formula (an application rule, really) for creating a full name. What is wrong with that, you wonder?
What if next week I get a call from the users: We want to see the names in first-space- last format. Now you must hunt through all my code for the last-comma-first constructions.
Finally, this very common query will likely appear in a variety of formats in multiple places in my application.
This SQL redundancy can make it very hard to maintain app logic and optimize its performance.
I would like to be able to change the way I write my code to avoid the above hardcodings.
To do that, I need to write these things once (one definition of a "full name" datatype,
one representation of the formula, one version of the query) and then call them wherever needed.
Packages can solve this challenge.
Consider the following package specification:
/* Files on web: fullname.pkg, fullname.tst */
1 PACKAGE employee_pkg
3 SUBTYPE fullname_t IS VARCHAR2 (200);
5 FUNCTION fullname (
6 last_in employees.last_name%TYPE,
7 first_in employees.first_name%TYPE)
8 RETURN fullname_t;
10 FUNCTION fullname (
11 employee_id_in IN employees.employee_id%TYPE)
12 RETURN fullname_t;
13 END employee_pkg;
What I have done here is essentially list the different elements I want to use.
The following table summarizes the important elements of the code.
The following notes in the table below are with respect to the red rectangles in the syntax diagram above.
Specify the same package name here that was used in your package specification command.
Each public sub-program is defined here. Parameters must match the package specifications. Separate the sub-programs using the END statement
followed by a semicolon. In addition to the public sub-programs, additional private sub-programs, cursors, variables, and exceptions can be defined here.