Sub topic 7
Overloading Subprograms in a Package
Assume you know basic SQL, PL/SQL variables and control flow, standalone procedures and functions, and basic exception handling. This chapter focuses on how one package can expose several subprograms with the same name.
What overloading means
Overloading means declaring multiple procedures or functions with the same name but different parameter signatures. A signature includes the number, order, and compatible datatypes of the parameters.
The compiler examines the arguments at each call and chooses the matching declaration. Overloading is useful when operations are conceptually the same but need to accept different kinds of input.
For example, formatting a number and formatting a date are both to_text operations:
Declaring overloads in the specification
The package specification publishes both versions. Callers see the same function name twice, but each declaration has a different parameter datatype.
CREATE OR REPLACE PACKAGE fmt AS
FUNCTION to_text(p_value IN NUMBER) RETURN VARCHAR2;
FUNCTION to_text(p_value IN DATE) RETURN VARCHAR2;
END fmt;
/The package body must implement every public declaration. Each implementation repeats the matching parameter list and return datatype.
CREATE OR REPLACE PACKAGE BODY fmt AS
FUNCTION to_text(p_value IN NUMBER) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'FM999G999G990D00');
END to_text;
FUNCTION to_text(p_value IN DATE) RETURN VARCHAR2 IS
BEGIN
RETURN TO_CHAR(p_value, 'YYYY-MM-DD');
END to_text;
END fmt;
/A caller uses the same name for both versions:
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_text(1234.5));
DBMS_OUTPUT.PUT_LINE(fmt.to_text(TO_DATE('2026-11-28', 'YYYY-MM-DD')));
END;
/The numeric literal matches the NUMBER overload. The result of TO_DATE matches the DATE overload.
Rules and common errors
Overloads must differ in their input parameter lists. The following differences can select an overload:
- Different numbers of parameters
- Different parameter datatypes
- Different datatype families, where PL/SQL can distinguish the arguments
Return type alone cannot distinguish overloaded functions. This declaration is invalid because the parameter lists are identical:
FUNCTION convert_value(p_value IN NUMBER) RETURN VARCHAR2;
FUNCTION convert_value(p_value IN NUMBER) RETURN DATE;The parameter mode also cannot be the only difference. IN, OUT, and IN OUT do not provide a safe way to create separate overloads with otherwise identical parameters.
An untyped NULL can cause ambiguity because it can be converted to several datatypes:
-- This may be ambiguous:
-- fmt.to_text(NULL);Convert the value explicitly when the intended overload is unclear:
BEGIN
DBMS_OUTPUT.PUT_LINE(fmt.to_text(CAST(NULL AS NUMBER)));
END;
/Implicit conversions can also produce surprising results. A character value might be converted to either a number or a date depending on the available overloads and session settings. Pass values with the intended datatype instead of relying on implicit conversion.
Design practice
Keep overloads consistent. They should return the same general kind of result and perform the same conceptual operation for different input types. Put the public overload declarations in the specification and keep helper logic private in the body.
A package can also overload procedures, not only functions. For example, send_message(VARCHAR2) and send_message(CLOB) may share a name when both perform the same kind of action.
Exercise
Why can two functions with identical parameter lists not be overloaded using different return types?
Show answer
PL/SQL chooses an overload from the input arguments before it has a return value, so return type alone cannot identify which function to call.