Creating Packages   «Prev  Next»

Lesson 6Executing Components of a Package
ObjectiveCreate a PL/SQL block that calls different parts of a package.

Executing Components of a Package

Testing a package involves testing each of the public procedures and public functions defined in the package specifications by executing them individually. Private functions and private procedures are tested at the same time, because these are both used when you execute the public functions and public procedures.
Executing a public procedure contained in a package is similar to testing a stand-alone procedure, except you must add the package name as a qualifier to the procedure name. For example, a package named GET_CALCULATION has a procedure named TOTAL_SALARY. To call the procedure, you must use the name GET_CALCULATION.TOTAL_SALARY.
Follow along with this Simulation to see how to call a procedure and a function that are part of the same package.

Execute Package Procedure

The mechanics of calling a procedure and function that are stored within a package are similar to using stand-alone procedures and functions.
Now you can try out the package you created in the previous lesson. In this exercise, use the sample results shown at the end of the exercise.
The next lesson contains the module conclusion

Steps To Execute Procedure Package

Click the link below to read about the steps to execute Procedure Package.
Steps To Execute Procedure Package

Package Body

The package body contains all the code required to implement the package specification.
A package body is required when any of the following conditions are 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 want to execute code in the initialization section of the package. 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:
  1. 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.
  2. 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. I discuss this topic in the following section.
  3. 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.
  4. A package body may consist of the following combinations: only a declaration section; only an execution section; execution and exception sections; or declaration, execution, and exception sections.
  5. 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.
  6. The same rules and restrictions for declaring package-level data structures apply to the body as well as to the specification, for example, you cannot declare a cursor variable.
  7. You can include an optional package name label after the END statement of the package body, as in:

Executing Package Components - Exercise

On Your Own Exercise: Build the PL/SQL block at home on your own database.
Executing Package Components - Exercise