Wednesday, January 18, 2017

PL/SQL Package

Introducing to PL/SQL Package

PL/SQL package is a group of related functionsprocedurestypescursors, etc. PL/SQL package is like a library once written stored in the Oracle database and can be used by many applications.
A PL/SQL package has two parts: package specification and package body.
  • A package specification is the public interface of your applications. The public means the stored function, procedures, types, etc., are accessible from other applications.
  • A package body contains the code that implements the package specification.
PL/SQL Package
PL/SQL Package

Creating PL/SQL Package Specification

The package specification is required when you create a new package. The package specification lists all the objects which are publicly accessible from other applications. The package specification also provides the information that developers need to know in order to use the interface. In short, package specification is the package’s API.
If the package specification does not contain any stored functions, procedures and no private code is needed, you don’t need to have a package body. These packages may contain only type definition and variables declaration. Those variables are known as package data. The scope of package data is global to applications. It is recommended that you should hide as much as package data as possible and use get and set functions to read and write that data. By doing this, you can prevent your package data changed unintentionally.
It is important to note that you must compile the package specification before package body.
Here is the syntax of creating PL/SQL package specification:
The CREATE PACKAGE statement is used to define a new package specification. If you want to rebuild existing package you can use the REPLACE keyword instead of the CREATE keyword. In the package specification, you can define new types, declare global variables, types, objects, exceptions, cursors, procedures, and functions.
Below is an example of creating a new package specification called personnelThe personnelpackage contains two functions: get_fullname() and get_salary() based on employee’s ID.

Creating PL/SQL Package Body

PL/SQL package body contains all the code that implements stored functions, procedures, and cursors listed in the package specification.
The following illustrates the syntax of creating package body:
The syntax is similar to the package specification except the keyword BODY and the implemented code of package specification.
The package body can contain an optional package initialization section that appears at the end of the package body. The package initialization sections start with the BEGIN keyword and end with the EXCEPTION section or END keyword of the package. The package initialization section is executed when the application references to the package element at the first time.
The following illustrates package body of the personnel package:

Referencing PL/SQL package elements

You reference to package elements by using dot notation:
The following code illustrates how to use personnel package by calling its functions:
In this tutorial, you’ve learned how to create a complete PL/SQL package and how to use its functions in another program.

No comments:

Post a Comment

Oracle Fusion - Cost Lines and Expenditure Item link in Projects

SELECT   ccd.transaction_id,ex.expenditure_item_id,cacat.serial_number FROM fusion.CST_INV_TRANSACTIONS cit,   fusion.cst_cost_distribution_...