Signup/Sign In

Packages in PL/SQL

A package is a way of logically storing the subprograms like procedures, functions, exception or cursor into a single common unit.

A package can be defined as an oracle object that is compiled and stored in the database.

Once it is compiled and stored in the database it can be used by all the users of database who have executable permissions on Oracle database.

Components of Package

Package has two basic components:

  • Specification: It is the declaration section of a Package
  • Body: It is the definition section of a Package.

Benefits of using Package

Following are some of the benefits of packages in PL/SQL:

  1. REUSABILITY

    Whenever a package is created, it is compiled and stored in the database. So, you write the code once which can be reused by other applications.

  2. OVERLOADING

    Two or more >procedures or functions can be created in a package with the same name.

  3. CREATING MODULES

    A large application can be created by simply creating modules(or subprograms) clearly defined and easy to work.

  4. IMPROVES PERFORMANCE

    Package code gets loaded inside the SGA(system global area) of Oracle at first call itself due to which other subsequent calls will work very fast.

  5. GLOBAL DECLARATION

    If the objects(procedures, functions, variables, constants, exceptions, cursor etc) are declared globally in a package, they can be easily used when required.


How to create a PL/SQL Package?

Following are the steps to declare and use a package in PL/SQL code block:

STEP 1: Package specification or declaration

It mainly comprises of the following:

  • Package Name.
  • Variable/constant/cursor/procedure/function/exception declaration.
  • This declaration is global to the package.

Here is the syntax:

CREATE OR REPLACE PACKAGE <package_name> IS/AS
	FUNCTION <function_name> (<list of arguments>) 
	RETURN <datatype>;
	PROCEDURE <procedure_name> (<list of arguments>);
	-- code statements
END <package_name>;

where,

CREATE OR REPLACE PACKAGE are keywords used to create a package

FUNCTION and PROCEDURE are keywords used to declare function and procedure while creating package.

<package_name>, <function_name>, <procedure_name> are user-defined names.

IS/AS are keywords used to declare package.

RETURN is a keyword specifying value returned by the function declared.

STEP 2: Package Body

It mainly comprises of the following:

  • It contains the definition of procedure, function or cursor that is declared in the package specification.
  • It contains the subprogram bodies containing executable statements for which package has been created

Here is the syntax:

CREATE  OR REPLACE PACKAGE BODY <package_name> IS/AS
FUNCTION <function_name> (<list of arguments>) RETURN <datatype>IS/AS
	-- local variable declaration;
	BEGIN
		-- executable  statements;
	EXCEPTION
		-- error handling statements;
END <function_name>;

PROCEDURE <procedure_name> (<list of arguments>)IS/AS
	-- local variable declaration;
	BEGIN
		-- executable statements;
	EXCEPTION
		-- error handling statements;
	END <procedure_name>;
END <package_name>;

Where,

CREATE OR REPLACE PACKAGE BODY are keywords used to create the package with a body.

FUNCTION and PROCEDURE are keywords used to define function and procedure while creating package.

<package_name>, <function_name>, <procedure_name> are user-defined.

IS/AS are keywords used to define the body of package, function and procedure.

RETURN is a keyword specifying value returned by the function defined.

DECLARE, BEGIN, EXCEPTION, END are the different sections of PL/SQL code block containing variable declaration, executable statements, error handling statements and marking end of PL/SQL block respectively where DECLARE and EXCEPTION part are optional.

Note: Creating a package only defines it, to use it we must refer it using the package object.

Following is the syntax for referring a package object:

Packagename.objectname;

The Object can be a function, procedure, cursor, exception that has been declared in the package specification and defined in the package body and to access their executable statements above syntax is used.

Time for an Example!

We have a STUDENT table as specified below:

ROLLNO SNAME AGECOURSE
11 Anu 20 BSC
12 Asha 21 BCOM
13 Arpit 18 BCA
14 Chetan 20 BCA
15 Nihal 19 BBA

Let's write a simple program to demonstrate the use of Package in PL/SQL.

PL/SQL code for package specification:

CREATE OR REPLACE PACKAGE pkg_student IS
	PROCEDURE  updateRecord(sno student.rollno%type);
	FUNCTION deleteRecord(snm student.sname%type)
		RETURN boolean;
END pkg_student;

Package Created

PL/SQL code for package body:

 set serveroutput on;
CREATE OR REPLACE PACKAGE BODY pkg_student IS
	PROCEDURE updateRecord(sno student.rollno%type) IS
		BEGIN
			Update student set age=23 where rollno=sno;
			IF  SQL%FOUND THEN
				dbms_output.put_line('RECORD UPDATED');
			ELSE
				dbms_output.put_line('RECORD NOT FOUND');
			END IF;
		END updateRecord;

	FUNCTION deleteRecord(snm student.sname%type) RETURN boolean IS
		BEGIN
			Delete from student where sname=snm;
			RETURN SQL%FOUND;
		END deleteRecord;
END pkg_student;

Package Body Created


Now let's write the PL/SQL code for calling the Procedure and Function used in Package.

 set serveroutput on;
DECLARE
	sno student.rollno%type;
	s_age student.age%type;
	snm student.sname%type;
BEGIN
	sno := &sno;
	snm := &snm
	pkg_student.updateRecord(sno);
	IF pkg_student.deleteRecord(snm) THEN
		dbms_output.put_line('RECORD DELETED');
	ELSE
		dbms_output.put_line('RECORD NOT FOUND');
	END IF;
END;

Enter value for sno: 12 Enter value for snm: Neha RECORD UPDATED RECORD NOT FOUND PL/SQL procedure successfully completed.

Note: If the package specification or package body has been created with compilation errors then a following warning message is displayed on the screen:

WARNING: Package Body created with compilation errors.

In that case, the errors can be seen by executing following statement:

 SHOW ERRORS;

In the next tutorial we will cover transactions in PL/SQL.