Name
CREATE BODY PACKAGE -- define a new package body
Synopsis
CREATE [ OR REPLACE ] PACKAGE BODY name { IS | AS } [ declaration; ] [, ...] [ { PROCEDURE proc_name [ (argname [ IN | IN OUT | OUT ] argtype [ DEFAULT value ] [, ...]) ] { IS | AS } program_body END [ proc_name ]; | FUNCTION func_name [ (argname [ IN | IN OUT | OUT ] argtype [ DEFAULT value ] [, ...]) ] RETURN rettype { IS | AS } program_body END [ func_name ]; } ] [, ...] [ BEGIN statement; [, ...] ] END [ name ]
Description
CREATE PACKAGE BODY defines a new package body. CREATE OR REPLACE PACKAGE BODY will either create a new package body, or replace an existing body.
If a schema name is included, then the package body is created in the specified schema. Otherwise it is created in the current schema. The name of the new package body must match an existing package specification in the same schema. The new package body name must not match any existing package body in the same schema unless the intent is to update the definition of an existing package body, in which case use CREATE OR REPLACE PACKAGE BODY.
See Sections 6.1.2 and 6.2.2 for more information on the package body.
Parameters
name
The name (optionally schema-qualified) of the package body to create.
declaration
A private variable, type, cursor, or REF CURSOR declaration.
proc_name
The name of a public or private procedure. If proc_name exists in the package specification with an identical signature, then it is public, otherwise it is private.
argname
The name of an argument.
IN | IN OUT | OUT
The argument mode.
argtype
The data type(s) of the program’s arguments.
DEFAULT value
Default value of an input argument.
program_body
The declarations and SPL statements that comprise the body of the function or procedure.
func_name
The name of a public or private function. If func_name exists in the package specification with an identical signature, then it is public, otherwise it is private.
rettype
The return data type.
statement
An SPL program statement. Statements in the package initialization section are executed once per session the first time the package is referenced.
Examples
The following is the package body for the empinfo package.
CREATE OR REPLACE PACKAGE BODY empinfo IS v_counter INTEGER; PROCEDURE get_name ( p_empno NUMBER ) IS BEGIN SELECT ename INTO emp_name FROM emp WHERE empno = p_empno; v_counter := v_counter + 1; END; FUNCTION display_counter RETURN INTEGER IS BEGIN RETURN v_counter; END; BEGIN v_counter := 0; DBMS_OUTPUT.PUT_LINE('Initialized counter'); END;
The following two anonymous blocks execute the procedure and function in the empinfo package and display the public variable.
BEGIN empinfo.get_name(7369); DBMS_OUTPUT.PUT_LINE('Employee Name : ' || empinfo.emp_name); DBMS_OUTPUT.PUT_LINE('Number of queries: ' || empinfo.display_counter); END; Initialized counter Employee Name : SMITH Number of queries: 1 BEGIN empinfo.get_name(7900); DBMS_OUTPUT.PUT_LINE('Employee Name : ' || empinfo.emp_name); DBMS_OUTPUT.PUT_LINE('Number of queries: ' || empinfo.display_counter); END; Employee Name : JAMES Number of queries: 2
See Also