The package body holds the implementation details of a package. It can also contain private objects — variables, types, and subprograms — that are not visible to callers outside the package.
Prerequisites
Before you begin, ensure that you have:
-
A package specification created with the same
package_name
Syntax
CREATE [ OR REPLACE ] PACKAGE BODY package_name
{ IS | AS }
[ private_declaration; ] ...
[ { PROCEDURE proc_name [ ( argument_declaration [, ...] ) ]
[ options_list ]
{ IS | AS }
[ declaration; ] [, ...]
BEGIN
statement; [...]
[ EXCEPTION
{ WHEN exception [ OR exception ] [...] THEN statement; }
[...]
]
END [ proc_name ]; |
FUNCTION func_name [ ( argument_declaration [, ...] ) ]
RETURN rettype [ DETERMINISTIC ]
[ options_list ]
{ IS | AS }
[ declaration; ] [, ...]
BEGIN
statement; [...]
[ EXCEPTION
{ WHEN exception [ OR exception ] [...] THEN statement; }
[...]
]
END [ func_name ]; } ] [, ...]
[ BEGIN
statement; [...]
END; ]
[ package_name ] ;
Where options_list is one or more of:
STRICT
LEAKPROOF
COST execution_cost
ROWS result_rows
SET config_param { TO value | = value | FROM CURRENT }
Where argument_declaration is:
argname [ IN | IN OUT | OUT ] argtype [ DEFAULT value ]
ISandASare interchangeable throughout the syntax.
Parameters
| Parameter | Description |
|---|---|
package_name |
The name of the package. A package specification with this name must exist before you create the body. |
private_declaration |
A private item accessible to any procedure or function within the package, but not visible outside it. Valid declarations include: variable declarations, record declarations, collection declarations, REF CURSOR and CURSOR variable declarations, TYPE definitions for records, collections, and REF CURSORs, exception declarations, and object variable declarations. |
proc_name |
The name of the procedure to create. |
func_name |
The name of the function to create. |
rettype |
The return data type of the function. Accepts any type valid for argtype. A length cannot be specified — for example, use VARCHAR2, not VARCHAR2(10). |
DETERMINISTIC |
Marks the function as always returning the same result for the same input values. A DETERMINISTIC function cannot modify the database. Notes: (1) DETERMINISTIC is equivalent to the IMMUTABLE option in PostgreSQL. (2) If you specify DETERMINISTIC on a public function in the package body, you must also specify DETERMINISTIC in the corresponding package specification declaration. Private functions have no specification declaration. |
PRAGMA AUTONOMOUS_TRANSACTION |
Marks the procedure or function as an autonomous transaction. |
declaration |
A variable, type, REF CURSOR, or subprogram declaration in the procedure or function body. Subprogram declarations must appear after all variable, type, and REF CURSOR declarations. |
statement |
A Structured Process Language (SPL) program statement. A DECLARE - BEGIN - END block counts as a single SPL statement, so procedure and function bodies can contain nested blocks. |
exception |
An exception condition name, such as NO_DATA_FOUND or OTHERS. |
argname |
The name of a formal argument. The procedure or function body references the argument by this name. |
IN | IN OUT | OUT |
The argument mode. IN accepts input only (default). IN OUT accepts input and returns a value. OUT returns a value only. |
argtype |
The data type of an argument. Can be a basic data type, a column type reference using %TYPE (for example, tablename.columnname%TYPE), or a user-defined type such as a nested table or object type. Basic data types cannot include a length specifier. |
DEFAULT value |
A default value for an IN argument when the caller omits it. Not valid for IN OUT or OUT arguments. |
package_initializer |
Statements in the optional trailing BEGIN...END block. These statements run once per session the first time the package is referenced. |
PolarDB extensions (not supported by Oracle)
The following options extend the Oracle package body syntax and are available in ApsaraDB PolarDB only:
| Option | Description |
|---|---|
STRICT |
The function returns NULL without executing if any argument is NULL. |
LEAKPROOF |
The function does not reveal argument information through any channel other than its return value. |
PARALLEL { UNSAFE | RESTRICTED | SAFE } |
Controls parallel execution. UNSAFE (default) forces serial execution. RESTRICTED allows parallel execution but limits it to the parallel group leader. SAFE allows unrestricted parallel execution. |
COST execution_cost |
The estimated execution cost as a positive number in units of cpu_operator_cost. For set-returning functions, this is the cost per returned row. Default: 0.0025. |
ROWS result_rows |
The estimated number of rows the function returns. Used by the query planner. Default: 1000. |
SET config_param { TO value | = value | FROM CURRENT } |
Sets a configuration parameter for the duration of the function. FROM CURRENT restores the parameter value when the function exits. |
What's next
-
Package specification syntax — define the public interface before creating the package body
-
CREATE PACKAGE — reference for the
CREATE PACKAGEstatement -
DROP PACKAGE — remove a package and its body