Package components v17
Packages consist of two main components:
The package specification, which is the public interface. You can reference these elements outside the package. Declare all database objects that are a part of a package in the specification.
The package body, which contains the actual implementation of all the database objects declared in the package specification.
The package body implements the specifications in the package specification. It contains implementation details and private declarations that are invisible to the application. You can debug, enhance, or replace a package body without changing the specifications. Similarly, you can change the body without recompiling the calling programs because the implementation details are invisible to the application.
Package specification syntax
The package specification defines the user interface for a package (the API). The specification lists the functions, procedures, types, exceptions, and cursors that are visible to a user of the package.
The syntax used to define the interface for a package is:
Where authorization_clause
:=
Where procedure_or_function_declaration
:=
Where procedure_declaration
:=
Where function_declaration
:=
Where argument_list
:=
Where argument_declaration
:=
Where restriction_pragma
:=
Where restrictions
:=
Parameters
package_name
package_name
is an identifier assigned to the package. Each package must have a unique name in the schema.
AUTHID DEFINER
If you omit the AUTHID
clause or specify AUTHID DEFINER
, the privileges of the package owner are used to determine access privileges to database objects.
AUTHID CURRENT_USER
If you specify AUTHID CURRENT_USER
, the privileges of the current user executing a program in the package are used to determine access privileges.
declaration
declaration
is an identifier of a public variable. You can access a public variable from outside the package using the syntax package_name.variable
. There can be zero, one, or more public variables. Public variable definitions must come before procedure or function declarations.
declaration
can be any of the following:
- Variable declaration
- Record declaration
- Collection declaration
REF CURSOR
and cursor variable declarationTYPE
definitions for records, ollections, andREF CURSOR
- Exception
- Object variable declaration
proc_name
The name of a public procedure.
argname
The name of an argument. The argument is referenced by this name in the function or procedure body.
IN
| IN OUT
| OUT
The argument mode. IN
(the default) declares the argument for input only. IN OUT
allows the argument to receive a value as well as return a value. OUT
specifies the argument is for output only.
argtype
The data types of an argument. An argument type can be a base data type, a copy of the type of an existing column using %TYPE
, or a user-defined type such as a nested table or an object type. Don't specify a length for any base type. For example, specify VARCHAR2
, not VARCHAR2(10)
.
Reference the type of a column by writing tablename.columnname %TYPE
. Using this nomenclature can sometimes help make a procedure independent from changes to the definition of a table.
DEFAULT value
The DEFAULT
clause supplies a default value for an input argument if you don't supply one in the invocation. You can't specify DEFAULT
for arguments with modes IN OUT
or OUT
.
func_name
The name of a public function.
rettype
The return data type.
DETERMINISTIC
DETERMINISTIC
is a synonym for IMMUTABLE
. A DETERMINISTIC
function can't modify the database and always reaches the same result when given the same argument values. It doesn't do database lookups or otherwise use information not directly present in its argument list. If you include this clause, any call of the function with all-constant arguments can be immediately replaced with the function value.
restriction
The following keywords are accepted for compatibility and ignored:
RNDS
RNPS
TRUST
WNDS
WNPS
Package body syntax
Package implementation details reside in the package body. The package body can contain objects that aren't visible to the package user. EDB Postgres Advanced Server supports the following syntax for the package body: