.


:




:

































 

 

 

 


The Package Body




The package body contains all the code required to implement the package specification. A package body is not always needed; see Section 17.5 for examples of package specifications without bodies. A package body is required when any of the following conditions is true:

 

The package specification contains a cursor declaration with a RETURN clause

You will then need to specify the SELECT statement in the package body.

 

The package specification contains a procedure or function declaration

You will then need to complete the implementation of that module in the package body.

 

You wish to execute code in the initialization section of the package body

The package specification does not support an execution section (executable statements within a BEGIN...END); you can do this only in the body.

Structurally, a package body is very similar to a procedure definition. Here are some rules particular to package bodies:

A package body can have declaration, execution, and exception sections. The declaration section contains the complete implementation of any cursors and programs defined in the specification, and also the definition of any private elements (not listed in the specification). The declaration section can be empty as long as there is an initialization section.

The execution section of a package is known as the initialization section; this optional code is executed when the package is instantiated for a session. We discuss this topic in the following section.

The exception section handles any exceptions raised in the initialization section. You can have an exception section at the bottom of a package body only if you have defined an initialization section.

A package body may consist of the following combinations: just a declaration section; just an execution section; execution and exception sections; or declaration, execution, and exception sections.

You may not include an AUTHID clause in the package body; it must go in the package specification. Anything declared in the specification may be referenced (used) within the package body.

The same rules and restrictions for declaring package-leveldata structures apply to the body as well as to the specificationfor example, you cannot declare a cursor variable.

You can include an optional package name label after the END statement of the package, as in:

END my_package;

Here is an implementation of the favorites_pkg body:

CREATE OR REPLACE PACKAGE BODY favorites_pkgIS -- A private variable g_most_popular PLS_INTEGER:= c_strawberry; -- Implementation of procedure PROCEDURE show_favorites (list_in IN codes_nt) IS BEGIN FOR indx IN list_in.FIRST.. list_in.LAST LOOP DBMS_OUTPUT.put_line (list_in (indx)); END LOOP; END show_favorites; -- Implement the function FUNCTION most_popular RETURN fav_info_rct IS retval fav_info_rct; null_cv fav_info_rct; BEGIN OPEN retval FOR SELECT * FROM favorites WHERE code = g_most_popular; RETURN retval; EXCEPTION WHEN NO_DATA_FOUND THEN RETURN null_cv; END most_popular; END favorites_pkg; -- End label for package

See Section 17.5 for other examples of package bodies.





:


: 2015-10-01; !; : 331 |


:

:

, .
==> ...

1581 - | 1436 -


© 2015-2024 lektsii.org - -

: 0.011 .