Sub topic 4
Implementing the PL/SQL Package Body
This chapter assumes you know basic SQL, PL/SQL variables and control flow, standalone procedures and functions, and basic exception handling. A package specification declares the public interface. The package body supplies the executable implementation and can also contain private declarations.
What belongs in the body
The body must implement every procedure and function declared in the specification. Each implementation must match its public declaration, including:
- The subprogram name.
- The parameter names and datatypes.
- The parameter modes, such as
INorOUT. - The return datatype for a function.
The body can also declare private constants, variables, cursors, procedures, and functions. These declarations are invisible to callers because they do not appear in the specification.
The body is a separate stored object. Oracle compiles it against the specification and the objects referenced by its implementation.
| Package part | Main purpose | Visible to callers |
|---|---|---|
| Specification | Declare the public contract | Yes |
| Body | Implement public subprograms and hold private code | Only public declarations |
Implementing public subprograms
The following specification declares a public constant, exception, function, and procedure. The body implements the function and procedure and adds a private validation function.
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;
/
CREATE OR REPLACE PACKAGE BODY emp_api AS
FUNCTION is_valid_pct(p_pct IN NUMBER) RETURN BOOLEAN IS
BEGIN
RETURN p_pct IS NOT NULL
AND p_pct > 0
AND p_pct <= c_max_raise_pct;
END is_valid_pct;
FUNCTION get_salary(
p_employee_id IN employees.employee_id%TYPE
) RETURN employees.salary%TYPE IS
l_salary employees.salary%TYPE;
BEGIN
SELECT salary
INTO l_salary
FROM employees
WHERE employee_id = p_employee_id;
RETURN l_salary;
END get_salary;
PROCEDURE raise_salary(
p_employee_id IN employees.employee_id%TYPE,
p_pct IN NUMBER
) IS
BEGIN
IF NOT is_valid_pct(p_pct) THEN
RAISE e_raise_too_large;
END IF;
UPDATE employees
SET salary = salary * (1 + p_pct / 100)
WHERE employee_id = p_employee_id;
END raise_salary;
END emp_api;
/The body uses the public constant and exception through their simple names because they belong to the same package. A caller uses qualified names such as emp_api.c_max_raise_pct.
A function can contain local variables between IS or AS and BEGIN. Its executable statements go between BEGIN and END. The same structure applies to procedures.
Declaration order and private helpers
PL/SQL normally requires a subprogram to be declared before code references it. This rule applies to private subprograms in a package body.
Place a helper before the public procedure that calls it, as in is_valid_pct above. If two private subprograms call each other, use a forward declaration. A forward declaration gives the compiler a signature before the full implementation appears.
CREATE OR REPLACE PACKAGE BODY parity AS
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN;
FUNCTION is_even(p_n IN PLS_INTEGER) RETURN BOOLEAN IS
BEGIN
IF p_n = 0 THEN
RETURN TRUE;
END IF;
RETURN is_odd(p_n - 1);
END is_even;
FUNCTION is_odd(p_n IN PLS_INTEGER) RETURN BOOLEAN IS
BEGIN
IF p_n = 0 THEN
RETURN FALSE;
END IF;
RETURN is_even(p_n - 1);
END is_odd;
END parity;
/The first is_odd line is only a declaration. Its implementation appears later.
Common compilation errors
A body becomes invalid when its implementation does not match the specification or when a referenced dependency cannot be resolved. Common causes include:
- A public function is missing from the body.
- A procedure has the wrong parameter mode or datatype.
- A function has the wrong return datatype.
- A private helper is referenced before it is declared.
- A table, column, type, or other referenced object does not exist.
- The body contains a syntax or semantic error.
After compilation, inspect errors with your SQL tool. In SQL*Plus or SQLcl, use:
SHOW ERRORS PACKAGE BODY emp_apiChanging only the body usually does not change the public contract. Existing callers can continue to use the same package names if the body compiles successfully.
Exercise
Why can a caller use empapi.raisesalary but not empapi.isvalid_pct?
Show answer
raisesalary is declared in the package specification, while isvalid_pct is declared only in the package body and is therefore private.