Sub topic 2
Writing the PL/SQL Package Specification
This chapter assumes you know basic SQL, PL/SQL variables and control flow, anonymous blocks, standalone subprograms, and basic exception handling.
A package is a named schema object that groups related PL/SQL items. The package specification is its public interface. It declares what other PL/SQL code may use, but it does not contain the implementation of procedures and functions.
The specification is similar to a contract:
- It gives the package name.
- It declares public procedures and functions.
- It can expose constants, types, cursors, and exceptions.
- It controls which package elements callers can reference.
Declaring the public interface
Use CREATE OR REPLACE PACKAGE followed by the package name and an AS or IS keyword. End the declaration with END package_name and a slash on a separate line.
CREATE OR REPLACE PACKAGE emp_api AS
c_max_raise_pct CONSTANT NUMBER := 20;
e_raise_too_large EXCEPTION;
PRAGMA EXCEPTION_INIT(e_raise_too_large, -20001);
FUNCTION get_salary(
p_employee_id IN employees.employee_id%TYPE
) RETURN employees.salary%TYPE;
PROCEDURE raise_salary(
p_employee_id IN employees.employee_id%TYPE,
p_pct IN NUMBER
);
END emp_api;
/This specification exposes one constant, one named exception, one function, and one procedure. It does not say how get_salary retrieves a salary or how raise_salary updates a row. Those implementations belong in the package body.
A procedure declaration lists its parameters but has no executable statement. A function declaration also includes its return datatype. The declaration must end with a semicolon.
The parameter mode describes how data moves between the caller and the subprogram:
| Mode | Meaning |
|---|---|
| `IN` | The subprogram receives a value and cannot assign a new value to the caller's argument. |
| `OUT` | The subprogram returns a value through the parameter. |
| `IN OUT` | The subprogram receives a value and can return an updated value. |
IN is the default mode, but writing it explicitly makes the interface easier to read.
Choosing what to expose
Expose only items that callers need. Public declarations become part of the package contract. Removing or changing them can require dependent code to be recompiled or changed.
Use %TYPE when a parameter or return value should follow a table column's datatype:
FUNCTION get_salary(
p_employee_id IN employees.employee_id%TYPE
) RETURN employees.salary%TYPE;If the column datatype changes, this declaration can remain aligned with the table definition. It also avoids duplicating datatype details.
Constants provide named values instead of unexplained literals. Named exceptions let callers handle specific errors:
BEGIN
emp_api.raise_salary(100, 30);
EXCEPTION
WHEN emp_api.e_raise_too_large THEN
DBMS_OUTPUT.PUT_LINE('Raise is above the allowed limit');
END;
/Callers qualify public items with the package name and a dot. This prevents naming confusion and makes the source of an item clear.
Specification design rules
Keep related operations together. For example, an employee package might contain employee lookup and salary operations, while an invoice package contains invoice operations.
Use consistent parameter names and modes. Prefixing parameters with p_ and constants with c_ is a common convention, but follow the conventions already used in your codebase.
Avoid exposing package variables unless callers genuinely need shared state. Public variables can be changed directly, which makes validation and debugging harder. Prefer public procedures and functions that control access to internal data.
A package specification can be compiled before its body exists. This lets other objects compile against the intended interface, but callers cannot successfully use an unimplemented subprogram until the body is created.
Exercise
Which parts of emp_api can a caller reference directly: c_max_raise_pct, get_salary, raise_salary, and is_valid_pct if is_valid_pct is declared only in the body?
Show answer
The caller can reference c_max_raise_pct, get_salary, and raise_salary. It cannot reference is_valid_pct because an item declared only in the body is private.