Package is served to group together more PL/SQL objects, like procedures, functions, constants, variables, cursors, exception names and TYP statements.
Basic PACKAGE Syntax
CREATE [OR REPLACE] PACKAGE package_name
[ AUTHID { CURRENT_USER | DEFINER } ]
{ IS | AS }
[definitions of public TYPEs
,declarations of public variables, types, and objects
,declarations of exceptions
,pragmas
,declarations of cursors, procedures, and functions
,headers of procedures and functions]
END [package_name];
/
CREATE [OR REPLACE] PACKAGE BODY package_name
{ IS | AS }
[definitions of private TYPEs
,declarations of private variables, types, and objects
,full definitions of cursors
,full definitions of procedures and functions]
[BEGIN
executable_statements
[EXCEPTION
exception_handlers]]
END [package_name];
/
Example package
CREATE OR REPLACE PACKAGE time_pkg IS
FUNCTION GetTimestamp RETURN DATE;
PRAGMA RESTRICT_REFERENCES (GetTimestamp, WNDS);
PROCEDURE ResetTimestamp(new_time DATE
DEFAULT SYSDATE);
END time_pkg;
/
CREATE OR REPLACE PACKAGE BODY time_pkg IS
StartTimeStamp DATE := SYSDATE;
— StartTimeStamp is package data.
FUNCTION GetTimestamp RETURN DATE IS
BEGIN
RETURN StartTimeStamp;
END GetTimestamp;
PROCEDURE ResetTimestamp(new_time DATE DEFAULT SYSDATE)
IS
BEGIN
StartTimeStamp := new_time;
END ResetTimestamp;
END time_pkg;
/
Referencing package element is done as:
package_name.package_element
Leave a Reply