Sub topic 1
What a package is and why you use one
Prerequisites
This chapter assumes you already write basic SQL (SELECT, INSERT, UPDATE), PL/SQL anonymous blocks with variables and control flow, standalone stored procedures and functions, and exception handling with WHEN clauses. The examples use an employees table with employee_id and salary columns.
What a package is and why you use one
A package is a named schema object that groups related procedures, functions, types, constants, and variables. Instead of scattering standalone subprograms across a schema, you keep related code together under one name.
A package has two parts:
- The specification declares the public interface that callers can use.
- The body contains the implementation, including any private helpers.
Callers depend on the specification. Changes that stay inside the body usually do not force dependent objects to recompile.
Writing the package specification
The specification lists what callers may use. It holds declarations only, with no implementation code. Each declared subprogram in the specification must have a matching definition in the body.
Public constants, types and exceptions in the specification
Anything declared in the specification is public. This includes constants, types, and exceptions. The PRAGMA EXCEPTION_INIT directive binds a named exception to an Oracle error number, so callers can handle it by name.
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;
/> Note: the specification is the contract. Keep it small and stable.
Implementing the package body
The body defines every subprogram the specification declares, with matching parameters and return types. It can also add private helpers that the specification does not declare. Callers cannot reach those helpers.
CREATE OR REPLACE PACKAGE BODY emp_api AS
-- Private: not declared in the specification.
FUNCTION is_valid_pct(p_pct IN NUMBER) RETURN BOOLEAN IS
BEGIN
RETURN 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;
/Callers use dot notation, such as emp_api.get_salary(100). They cannot call is_valid_pct because the specification does not declare it.
Exercise
Exercise
If the specification declares raise_salary but the body omits it, what happens when you compile the body?
Show answer
The body fails to compile. Every subprogram declared in the specification must be defined in the body with a matching signature.