Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

APEX INSTITUTE OF TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Database Management System (CST-227)


Faculty: Mr. Pramod Vishwakarma (E9758)

Packages in PL/SQL DISCOVER . LEARN . EMPOWER


1
DBMS: Course Objectives
COURSE OBJECTIVES
The Course aims to:
• Understand database system concepts and design databases for different applications
and to acquire the knowledge on DBMS and RDBMS.
• Implement and understand different types of DDL, DML and DCL statements.
• Understand transaction concepts related to databases and recovery/backup
techniques required for the proper storage of data.

By: Pramod Vishwakarma (E9758) 2


COURSE OUTCOMES

On completion of this course, the students shall be able to:-


Understand the database concept, system architecture and role of database
CO1
administrator
CO2 Design database for an organization using relational model
Apply relational algebra and relational calculus to query the database of
CO3
organization
CO4 Implement the package, procedures and triggers
CO5 Understand the concept of transaction processing and concurrency  control

By: Pramod Vishwakarma (E9758) 3


Packages in PL/SQL
• A package is a way of logically storing the subprograms
like procedure, function, 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.
Creating Package
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.
Syntax: Package specification or declaration
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>;
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:
Syntax- Package Body
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.
Referring A Package Object
• Note: Creating a package only defines it, to use it we must
refer it using the package object.

• Syntax: 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.
Example: 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_Emp;

Output: Package is created.


PL/SQL code for package body:
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;
Calling the Procedure and Function
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;
Error!!!
• 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;
Benefits of using Package

Following are some of the benefits of packages in


PL/SQL:
• REUSABILITY

• OVERLOADING

• CREATING MODULES

• IMPROVES PERFORMANCE

• GLOBAL DECLARATION
References
• RamezElmasri and Shamkant B. Navathe, “Fundamentals of Database System”, The
Benjamin / Cummings Publishing Co.
• Korth and Silberschatz Abraham, “Database System Concepts”, McGraw Hall.
• C.J.Date, “An Introduction to Database Systems”, Addison Wesley.
• Thomas M. Connolly, Carolyn & E. Begg, “Database Systems: A Practical Approach
to Design, Implementation and Management”, 5/E, University of Paisley, Addison-
Wesley.

By: Pramod Vishwakarma (E9758) 17


References
• https://
docs.oracle.com/database/121/LNPLS/packages.htm

18
THANK YOU

For queries
Email: pramod.e9758@cuimail.in
19

You might also like