Sub topic 12
Organizing Subprograms in Packages and Overloading Them
Assume you understand stored procedures and functions, parameter modes, and anonymous PL/SQL blocks. A package groups related subprograms, types, constants, and variables under one name. A package has two parts:
- The specification is the public interface. It declares what other code can call.
- The body contains the implementation. It can also contain private helpers that callers cannot use directly.
Package specification and body
The specification should expose only the operations that belong to the package's public API. Every subprogram declared in the specification must have a matching implementation in the body.
CREATE OR REPLACE PACKAGE fmt AS
FUNCTION to_label (p_value IN NUMBER) RETURN VARCHAR2;
FUNCTION to_label (p_value IN DATE) RETURN VARCHAR2;
END fmt;
/
CREATE OR REPLACE PACKAGE BODY fmt AS
FUNCTION to_label (p_value IN NUMBER) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'FM999G999G990D00');
END to_label;
FUNCTION to_label (p_value IN DATE) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'YYYY-MM-DD');
END to_label;
END fmt;
/The package defines two functions with the same name. This is overloading, which means providing multiple subprograms with different parameter signatures. PL/SQL selects the matching version from the arguments supplied.
Call a packaged subprogram with the package name and a dot:
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_label(12500.5));
DBMS_OUTPUT.PUT_LINE(fmt.to_label(DATE '2026-11-28'));
END;
/The first call uses the NUMBER overload. The second uses the DATE overload.
| Design choice | Effect |
|---|---|
| Declare a subprogram in the specification | Other schemas and units can call it, if they have privileges |
| Define a subprogram only in the body | The subprogram remains private |
| Overload by parameter type or count | PL/SQL can choose among distinct signatures |
| Change only the return type | This does not create a valid overload |
Overloading rules and errors
Overloads must be distinguishable by their parameter lists. You cannot overload two functions that differ only in return type. Some combinations of closely related types can also produce ambiguous calls, especially when PL/SQL must implicitly convert a literal.
For example, this call can be unclear if several overloads accept compatible numeric types:
-- Prefer an explicitly typed variable when overload resolution is unclear.
DECLARE
v_amount NUMBER := 12500.5;
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_label(v_amount));
END;
/Packages also provide a stable name for related behavior. You can replace the body to change implementation details without changing callers, as long as the specification remains compatible. Changing the specification can invalidate dependent objects, so treat it as an interface change.
A package can also contain private procedures used by several public operations. This avoids repeating validation or formatting logic while keeping implementation details hidden.
Exercise
Why can a package overload two functions that accept NUMBER and DATE, but not two functions that differ only in return type?
Show answer
PL/SQL chooses an overload from the supplied parameters. The return value is known only after the call is selected, so return type alone cannot distinguish the functions.