PL/SQL Packages
• Chapter 9
• Introduction
What is a Package?
• A package is a schema object that groups
• procedures, functions, variables, and types.
Advantages of Packages
• Modularity
• Encapsulation
• Performance
• Security
• Maintainability
Package Specification
• Declares public components
• Acts as interface
• Contains variables, types, procedures,
functions
Package Specification – Example
• CREATE OR REPLACE PACKAGE pkg_name IS
• PROCEDURE p1;
• FUNCTION f1 RETURN NUMBER;
• END;
Package Body
• Contains implementation
• Defines logic
• Can have private components
Package Body – Example
• CREATE OR REPLACE PACKAGE BODY
pkg_name IS
• PROCEDURE p1 IS BEGIN NULL; END;
• FUNCTION f1 RETURN NUMBER IS BEGIN
RETURN 1; END;
• END;
Serially Reusable Packages
• Ensures fresh data per call
• Uses PRAGMA SERIALLY_REUSABLE
Variables in Packages
• Specification variables → Public
• Body variables → Private
Types in Packages
• Record types
• Collections
• Cursors
• Reference cursors
Functions & Procedures
• Declared in spec
• Implemented in body
• Support overloading
Definer vs Invoker Rights
• AUTHID DEFINER (default)
• AUTHID CURRENT_USER
Managing Packages
• USER_OBJECTS
• USER_SOURCE
• USER_DEPENDENCIES
Summary
• Packages improve structure
• Separate interface & implementation
• Widely used in PL/SQL