Sub topic 1
What a Package Is and Why You Use One
Prerequisites
This chapter assumes that you know basic SQL, including SELECT, INSERT, and UPDATE. You should also understand PL/SQL variables, control flow, anonymous blocks, standalone procedures and functions, and basic exception handling.
The purpose of a package
A PL/SQL package is a named schema object that groups related program elements. These elements can include procedures, functions, constants, types, exceptions, and variables.
For example, an employee package might contain operations for reading an employee's salary, changing that salary, and checking salary rules. The package gives these operations one logical name, such as emp_api.
A package has two parts:
- The package specification declares the elements that callers can use.
- The package body contains the implementation and any private elements.
The specification is the public interface. The body is the implementation behind that interface.
Why use a package instead of standalone subprograms?
A standalone procedure or function is useful when one operation has no strong relationship with other operations. A package becomes useful when several operations belong to one area of an application.
Packages provide several benefits:
- Organization: Related operations have one name and one location.
- Encapsulation: Callers see only the interface that you choose to publish.
- Consistent rules: Several public operations can use the same private validation or formatting logic.
- Controlled changes: You can often change the body without changing code that calls the package.
- Shared definitions: Constants, types, and exceptions can be declared once and reused by callers.
- A package namespace: Callers can distinguish an operation such as
emp_api.get_salaryfrom another operation with the same simple name.
Call a packaged subprogram with dot notation:
package_name.subprogram_nameThis naming style makes the purpose of a call clearer than a generic standalone name such as get_salary.
A small package example
The following package exposes two operations. The specification says what callers may use. The body explains how those operations work.
CREATE OR REPLACE PACKAGE message_api AS
PROCEDURE say_hello(p_name IN VARCHAR2);
FUNCTION format_name(p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2)
RETURN VARCHAR2;
END message_api;
/
CREATE OR REPLACE PACKAGE BODY message_api AS
PROCEDURE say_hello(p_name IN VARCHAR2) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, ' || p_name || '!');
END say_hello;
FUNCTION format_name(p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2)
RETURN VARCHAR2 IS
BEGIN
RETURN INITCAP(TRIM(p_first_name) || ' ' || TRIM(p_last_name));
END format_name;
END message_api;
/
BEGIN
message_api.say_hello(
message_api.format_name('ada', 'lovelace')
);
END;
/The anonymous block does not need to know how format_name builds the result. It only needs the function's name, parameters, and return type. This separation reduces the amount of implementation detail that calling code must understand.
A package is compiled as a schema object. The specification and body are related, but they are separate source units. The specification establishes the contract. The body must implement every public procedure and function declared by that contract.
When a package is the right choice
Use a package when operations share a domain, rules, data types, or internal helpers. Examples include customer management, order processing, payroll, and application configuration.
A package is usually unnecessary for one small, unrelated operation. In that case, a standalone procedure or function may be simpler. As the number of related operations grows, a package provides a clearer boundary and makes future maintenance easier.
Exercise
Which package part should a caller read to learn how to invoke a public procedure: the specification or the body?
Show answer
The specification. It declares the public procedure's name, parameters, and other interface details.