SlideShare a Scribd company logo
Prof. Neeraj Bhargava
Pooja Dixit
Department of Computer Science
School of Engineering & System Science
MDS, University Ajmer, Rajasthan, India
1
 A package is a collection of PL/SQL objects
grouped together under one package name.
 Packages provide a means to collect related
procedures, functions, cursors, declarations,
types, and variables into a single, named
database object that is more flexible than the
related database objects are by themselves.
 Package variables – can be referenced in any
procedure, function, (other object) defined
within a package.
2
 A package consists of a package specification and a
package body.
◦ The package specification, also called the package header.
◦ Declares global variables, cursors, exceptions, procedures, and
functions that can be called or accessed by other program units.
◦ A package specification must be a uniquely named database
object.
◦ Elements of a package can declared in any order. If element “A”
is referenced by another element, then element “A” must be
declared before it is referenced by another element. For
example, a variable referenced by a cursor must be declared
before it is used by the cursor.
 Declarations of subprograms must be forward
declarations.
◦ This means the declaration only includes the subprogram name
and arguments, but does not include the actual program code.
3
 Basically, a package is a named declaration
section.
◦ Any object that can be declared in a PL/SQL block
can be declared in a package.
◦ Use the CREATE OR REPLACE PACKAGE clause.
◦ Include the specification of each named PL/SQL
block header that will be public within the
package.
◦ Procedures, functions, cursors, and variables that
are declared in the package specification are
global.
 The basic syntax for a package is:
CREATE [OR REPLACE PACKAGE[ <package name> {AS|IS}
<variable declarations>;
<cursor declarations>;
<procedure and function declarations>;
END <package name>;
4
 To declare a procedure in a package – specify
the procedure name, followed by the
parameters and variable types:
PROCEDURE <procedure_name> (param1 param1datatype,
param2 param2datatype, ...);
 To declare a function in a package, you must
specify the function name, parameters and
return variable type:
FUNCTION <function_name> (param1 param1datatype,
param2 param2datatype, ...)
RETURN <return data type>;
5
 Contains the code for the subprograms and
other constructs, such as exceptions, declared in
the package specification.
 Is optional – a package that contains only
variable declarations, cursors, and the like, but
no procedure or function declarations does not
require a package body.
 Any subprograms declared in a package must be
coded completely in the package body. The
procedure and function specifications of the
package body must match the package
declarations including subprogram names,
parameter names, and parameter modes.
6
 Use the CREATE OR REPLACE PACKAGE
BODY clause to create a package body.
The basic syntax is:
CREATE [OR REPLACE] PACKAGE BODY
<package name> AS
<cursor specifications>
<subprogram specifications and code>
END <package name>;
7
CREATE OR REPLACE PACKAGE ManageEmployee AS
-- Global variable declarations go here
-- Procedure to find employees
PROCEDURE FindEmployee(
emp_ID IN employee.EmployeeID%TYPE,
emp_FirstName OUT employee.FirstName%TYPE,
emp_LastName OUT employee.LastName%TYPE);
-- Exception raised by FindEmployee
e_EmployeeIDNotFound EXCEPTION;
-- Function to determine if employee identifier is
valid
FUNCTION GoodIdentifier(
emp_ID IN employee.EmployeeID%TYPE)
RETURN BOOLEAN;
END ManageEmployee;
/
8
CREATE OR REPLACE PACKAGE BODY ManageEmployee AS
-- Procedure to find employees
PROCEDURE FindEmployee(
emp_ID IN employee.EmployeeID%TYPE,
emp_FirstName OUT employee.FirstName%TYPE,
emp_LastName OUT employee.LastName%TYPE ) AS
BEGIN
SELECT FirstName, LastName
INTO emp_FirstName, emp_LastName
FROM Employee
WHERE EmployeeID = emp_ID;
-- Check for existence of employee
IF SQL%ROWCOUNT = 0 THEN
RAISE e_EmployeeIDNotFound;
END IF;
END FindEmployee;
9
-- Function to determine if employee identifier is valid
FUNCTION GoodIdentifier(
emp_ID IN employee.EmployeeID%TYPE)
RETURN BOOLEAN
IS
v_ID_Count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_ID_Count
FROM Employee
WHERE EmployeeID = emp_ID;
-- return TRUE if v_ID_COUNT is 1
RETURN (1 = v_ID_Count);
EXCEPTION
WHEN OTHERS THEN
RETURN FALSE;
END GoodIdentifier;
END ManageEmployee;
10
DECLARE
v_FirstName employee.FirstName%TYPE;
v_LastName employee.LastName%TYPE;
search_ID employee.EmployeeID%TYPE;
BEGIN
ManageEmployee.FindEmployee (&search_ID, v_FirstName,
v_LastName);
DBMS_OUTPUT.PUT_LINE ('The employee name is: ' ||
v_LastName || ', ' || v_FirstName);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Cannot find an employee
with that ID.');
END;
/
11
 When the employee identifier is valid, the code
displays the employee name as shown here.
Enter value for search_id: '01885'
The employee name is: Bock, Douglas
PL/SQL procedure successfully completed.
 When the identifier is not valid, the exception
raised within the called procedure is propagated
back to the calling procedure and is trapped by
the EXCEPTION section’s WHEN OTHERS clause
and an appropriate message is displayed as
shown here.
Enter value for search_id: '99999'
Cannot find an employee with that ID.
PL/SQL procedure successfully completed.
12
 A cursor variable can make a cursor dynamic so that it is
reusable and sharable among different procedures and
functions such as those created as part of a package.
 A cursor variable has data type REF CURSOR. It is like a
pointer in the C language, and it points to a query work
area where a result set is stored.
 First you must define a REF CURSOR type.
 Next, you define a cursor variable of that type. In this
general syntactic example, the <return_type> object
represents a row in a database table.
TYPE ref_type_name IS REF CURSOR
[RETURN <return_type>];
 This provides an example of declaring a cursor variable
that can be used to process data rows for the equipment
table of the Madison Hospital database.
DECLARE
TYPE equipment_Type IS REF CURSOR
RETURN equipment%ROWTYPE;
cv_Equipment IN OUT equipment_Type;
13
/* PL SQL Example 13.16 File: ch13-16.sql */
CREATE OR REPLACE PACKAGE BODY ManageEquipment AS
-- Procedure to get a specific item of equipment
PROCEDURE OpenItem (cv_Equipment IN OUT equipment_Type,
p_EquipmentNumber IN CHAR) AS
BEGIN
-- Populate the cursor
OPEN cv_Equipment FOR
SELECT * FROM Equipment
WHERE EquipmentNumber = p_EquipmentNumber;
END OpenItem;
PROCEDURE FetchItem (cv_Equipment IN equipment_Type,
equipment_Row OUT equipment%ROWTYPE) AS
BEGIN
FETCH cv_Equipment INTO equipment_Row;
END FetchItem;
END ManageEquipment;
14
/* PL SQL Example 13.16 File: ch13-16.sql */
DECLARE
-- Declare a cursor variable of the REF CURSOR type
item_Cursor ManageEquipment.equipment_Type;
v_EquipmentNumber equipment.EquipmentNumber%TYPE;
equipment_Row equipment%ROWTYPE;
BEGIN
-- Assign a equipment number to the variable
v_EquipmentNumber := '5001';
-- Open the cursor using a variable
ManageEquipment.OpenItem (item_Cursor, v_EquipmentNumber);
-- Fetch the equipment data and display it
LOOP
ManageEquipment.FetchItem( item_Cursor, equipment_Row);
EXIT WHEN item_cursor%NOTFOUND;
DBMS_OUTPUT.PUT (equipment_Row.EquipmentNumber || ' ');
DBMS_OUTPUT.PUT_LINE (equipment_Row.Description);
END LOOP;
END;
5001 Computer, Desktop
PL/SQL procedure successfully completed.
15

More Related Content

What's hot

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
Lakshman Basnet
 
4. plsql
4. plsql4. plsql
4. plsql
Amrit Kaur
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
Nick Buytaert
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
Eryk Budi Pratama
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQL
Pooja Dixit
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
Vaibhav0
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
IshaRana14
 
Store procedures
Store proceduresStore procedures
Store procedures
Farzan Wadood
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
DataminingTools Inc
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
Swapnali Pawar
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
Arun Sial
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
rehaniltifat
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
rehaniltifat
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
Rayhan Chowdhury
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 
Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 

What's hot (20)

Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
4. plsql
4. plsql4. plsql
4. plsql
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
Function in PL/SQL
Function in PL/SQLFunction in PL/SQL
Function in PL/SQL
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
PL/SQL - CURSORS
PL/SQL - CURSORSPL/SQL - CURSORS
PL/SQL - CURSORS
 
Store procedures
Store proceduresStore procedures
Store procedures
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Exception handling in plsql
Exception handling in plsqlException handling in plsql
Exception handling in plsql
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
Sql commands
Sql commandsSql commands
Sql commands
 

Similar to Packages in PL/SQL

Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
Thuan Nguyen
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
sweetysweety8
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
Thuan Nguyen
 
Mysql creating stored function
Mysql  creating stored function Mysql  creating stored function
Mysql creating stored function
Prof.Nilesh Magar
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprogramsSimon Huang
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
Thuan Nguyen
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
Belal Arfa
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
priyaprakash11
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
jain.pralabh
 
SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
JeevananthamArumugam
 
Bioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperlBioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperl
Prof. Wim Van Criekinge
 
PLSQLIV.ppt
PLSQLIV.pptPLSQLIV.ppt
PLSQLIV.ppt
NagendranathMVSS
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
TheVerse1
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
InSync Conference
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
Prof.Nilesh Magar
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
Gary Myers
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
Thuan Nguyen
 
VHDL lecture 2.ppt
VHDL lecture 2.pptVHDL lecture 2.ppt
VHDL lecture 2.ppt
seemasylvester
 

Similar to Packages in PL/SQL (20)

Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Mysql creating stored function
Mysql  creating stored function Mysql  creating stored function
Mysql creating stored function
 
Oracle db subprograms
Oracle db subprogramsOracle db subprograms
Oracle db subprograms
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
 
Module04
Module04Module04
Module04
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
SQL Procedures & Functions
SQL Procedures & FunctionsSQL Procedures & Functions
SQL Procedures & Functions
 
Bioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperlBioinformatica 10-11-2011-p6-bioperl
Bioinformatica 10-11-2011-p6-bioperl
 
PLSQLIV.ppt
PLSQLIV.pptPLSQLIV.ppt
PLSQLIV.ppt
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
Tony jambu   (obscure) tools of the trade for tuning oracle sq lsTony jambu   (obscure) tools of the trade for tuning oracle sq ls
Tony jambu (obscure) tools of the trade for tuning oracle sq ls
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
 
VHDL lecture 2.ppt
VHDL lecture 2.pptVHDL lecture 2.ppt
VHDL lecture 2.ppt
 

More from Pooja Dixit

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
Pooja Dixit
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptx
Pooja Dixit
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptx
Pooja Dixit
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptx
Pooja Dixit
 
K-Map.pptx
K-Map.pptxK-Map.pptx
K-Map.pptx
Pooja Dixit
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptx
Pooja Dixit
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptx
Pooja Dixit
 
Gray Code.pptx
Gray Code.pptxGray Code.pptx
Gray Code.pptx
Pooja Dixit
 
Flip Flop.pptx
Flip Flop.pptxFlip Flop.pptx
Flip Flop.pptx
Pooja Dixit
 
Encoder.pptx
Encoder.pptxEncoder.pptx
Encoder.pptx
Pooja Dixit
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptx
Pooja Dixit
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptx
Pooja Dixit
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
Pooja Dixit
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptx
Pooja Dixit
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptx
Pooja Dixit
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptx
Pooja Dixit
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptx
Pooja Dixit
 
Decoders
DecodersDecoders
Decoders
Pooja Dixit
 
Three Address code
Three Address code Three Address code
Three Address code
Pooja Dixit
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
Pooja Dixit
 

More from Pooja Dixit (20)

Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
number system.pptx
number system.pptxnumber system.pptx
number system.pptx
 
Multiplexer.pptx
Multiplexer.pptxMultiplexer.pptx
Multiplexer.pptx
 
Logic Gates.pptx
Logic Gates.pptxLogic Gates.pptx
Logic Gates.pptx
 
K-Map.pptx
K-Map.pptxK-Map.pptx
K-Map.pptx
 
Karnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptxKarnaugh Map Simplification Rules.pptx
Karnaugh Map Simplification Rules.pptx
 
Half Subtractor.pptx
Half Subtractor.pptxHalf Subtractor.pptx
Half Subtractor.pptx
 
Gray Code.pptx
Gray Code.pptxGray Code.pptx
Gray Code.pptx
 
Flip Flop.pptx
Flip Flop.pptxFlip Flop.pptx
Flip Flop.pptx
 
Encoder.pptx
Encoder.pptxEncoder.pptx
Encoder.pptx
 
De-multiplexer.pptx
De-multiplexer.pptxDe-multiplexer.pptx
De-multiplexer.pptx
 
DeMorgan’s Theory.pptx
DeMorgan’s Theory.pptxDeMorgan’s Theory.pptx
DeMorgan’s Theory.pptx
 
Combinational circuit.pptx
Combinational circuit.pptxCombinational circuit.pptx
Combinational circuit.pptx
 
Boolean Algebra.pptx
Boolean Algebra.pptxBoolean Algebra.pptx
Boolean Algebra.pptx
 
Binary Multiplication & Division.pptx
Binary Multiplication & Division.pptxBinary Multiplication & Division.pptx
Binary Multiplication & Division.pptx
 
Binary addition.pptx
Binary addition.pptxBinary addition.pptx
Binary addition.pptx
 
Basics of Computer Organization.pptx
Basics of Computer Organization.pptxBasics of Computer Organization.pptx
Basics of Computer Organization.pptx
 
Decoders
DecodersDecoders
Decoders
 
Three Address code
Three Address code Three Address code
Three Address code
 
Cyrus beck line clipping algorithm
Cyrus beck line clipping algorithmCyrus beck line clipping algorithm
Cyrus beck line clipping algorithm
 

Recently uploaded

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 

Recently uploaded (20)

RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 

Packages in PL/SQL

  • 1. Prof. Neeraj Bhargava Pooja Dixit Department of Computer Science School of Engineering & System Science MDS, University Ajmer, Rajasthan, India 1
  • 2.  A package is a collection of PL/SQL objects grouped together under one package name.  Packages provide a means to collect related procedures, functions, cursors, declarations, types, and variables into a single, named database object that is more flexible than the related database objects are by themselves.  Package variables – can be referenced in any procedure, function, (other object) defined within a package. 2
  • 3.  A package consists of a package specification and a package body. ◦ The package specification, also called the package header. ◦ Declares global variables, cursors, exceptions, procedures, and functions that can be called or accessed by other program units. ◦ A package specification must be a uniquely named database object. ◦ Elements of a package can declared in any order. If element “A” is referenced by another element, then element “A” must be declared before it is referenced by another element. For example, a variable referenced by a cursor must be declared before it is used by the cursor.  Declarations of subprograms must be forward declarations. ◦ This means the declaration only includes the subprogram name and arguments, but does not include the actual program code. 3
  • 4.  Basically, a package is a named declaration section. ◦ Any object that can be declared in a PL/SQL block can be declared in a package. ◦ Use the CREATE OR REPLACE PACKAGE clause. ◦ Include the specification of each named PL/SQL block header that will be public within the package. ◦ Procedures, functions, cursors, and variables that are declared in the package specification are global.  The basic syntax for a package is: CREATE [OR REPLACE PACKAGE[ <package name> {AS|IS} <variable declarations>; <cursor declarations>; <procedure and function declarations>; END <package name>; 4
  • 5.  To declare a procedure in a package – specify the procedure name, followed by the parameters and variable types: PROCEDURE <procedure_name> (param1 param1datatype, param2 param2datatype, ...);  To declare a function in a package, you must specify the function name, parameters and return variable type: FUNCTION <function_name> (param1 param1datatype, param2 param2datatype, ...) RETURN <return data type>; 5
  • 6.  Contains the code for the subprograms and other constructs, such as exceptions, declared in the package specification.  Is optional – a package that contains only variable declarations, cursors, and the like, but no procedure or function declarations does not require a package body.  Any subprograms declared in a package must be coded completely in the package body. The procedure and function specifications of the package body must match the package declarations including subprogram names, parameter names, and parameter modes. 6
  • 7.  Use the CREATE OR REPLACE PACKAGE BODY clause to create a package body. The basic syntax is: CREATE [OR REPLACE] PACKAGE BODY <package name> AS <cursor specifications> <subprogram specifications and code> END <package name>; 7
  • 8. CREATE OR REPLACE PACKAGE ManageEmployee AS -- Global variable declarations go here -- Procedure to find employees PROCEDURE FindEmployee( emp_ID IN employee.EmployeeID%TYPE, emp_FirstName OUT employee.FirstName%TYPE, emp_LastName OUT employee.LastName%TYPE); -- Exception raised by FindEmployee e_EmployeeIDNotFound EXCEPTION; -- Function to determine if employee identifier is valid FUNCTION GoodIdentifier( emp_ID IN employee.EmployeeID%TYPE) RETURN BOOLEAN; END ManageEmployee; / 8
  • 9. CREATE OR REPLACE PACKAGE BODY ManageEmployee AS -- Procedure to find employees PROCEDURE FindEmployee( emp_ID IN employee.EmployeeID%TYPE, emp_FirstName OUT employee.FirstName%TYPE, emp_LastName OUT employee.LastName%TYPE ) AS BEGIN SELECT FirstName, LastName INTO emp_FirstName, emp_LastName FROM Employee WHERE EmployeeID = emp_ID; -- Check for existence of employee IF SQL%ROWCOUNT = 0 THEN RAISE e_EmployeeIDNotFound; END IF; END FindEmployee; 9
  • 10. -- Function to determine if employee identifier is valid FUNCTION GoodIdentifier( emp_ID IN employee.EmployeeID%TYPE) RETURN BOOLEAN IS v_ID_Count NUMBER; BEGIN SELECT COUNT(*) INTO v_ID_Count FROM Employee WHERE EmployeeID = emp_ID; -- return TRUE if v_ID_COUNT is 1 RETURN (1 = v_ID_Count); EXCEPTION WHEN OTHERS THEN RETURN FALSE; END GoodIdentifier; END ManageEmployee; 10
  • 11. DECLARE v_FirstName employee.FirstName%TYPE; v_LastName employee.LastName%TYPE; search_ID employee.EmployeeID%TYPE; BEGIN ManageEmployee.FindEmployee (&search_ID, v_FirstName, v_LastName); DBMS_OUTPUT.PUT_LINE ('The employee name is: ' || v_LastName || ', ' || v_FirstName); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE ('Cannot find an employee with that ID.'); END; / 11
  • 12.  When the employee identifier is valid, the code displays the employee name as shown here. Enter value for search_id: '01885' The employee name is: Bock, Douglas PL/SQL procedure successfully completed.  When the identifier is not valid, the exception raised within the called procedure is propagated back to the calling procedure and is trapped by the EXCEPTION section’s WHEN OTHERS clause and an appropriate message is displayed as shown here. Enter value for search_id: '99999' Cannot find an employee with that ID. PL/SQL procedure successfully completed. 12
  • 13.  A cursor variable can make a cursor dynamic so that it is reusable and sharable among different procedures and functions such as those created as part of a package.  A cursor variable has data type REF CURSOR. It is like a pointer in the C language, and it points to a query work area where a result set is stored.  First you must define a REF CURSOR type.  Next, you define a cursor variable of that type. In this general syntactic example, the <return_type> object represents a row in a database table. TYPE ref_type_name IS REF CURSOR [RETURN <return_type>];  This provides an example of declaring a cursor variable that can be used to process data rows for the equipment table of the Madison Hospital database. DECLARE TYPE equipment_Type IS REF CURSOR RETURN equipment%ROWTYPE; cv_Equipment IN OUT equipment_Type; 13
  • 14. /* PL SQL Example 13.16 File: ch13-16.sql */ CREATE OR REPLACE PACKAGE BODY ManageEquipment AS -- Procedure to get a specific item of equipment PROCEDURE OpenItem (cv_Equipment IN OUT equipment_Type, p_EquipmentNumber IN CHAR) AS BEGIN -- Populate the cursor OPEN cv_Equipment FOR SELECT * FROM Equipment WHERE EquipmentNumber = p_EquipmentNumber; END OpenItem; PROCEDURE FetchItem (cv_Equipment IN equipment_Type, equipment_Row OUT equipment%ROWTYPE) AS BEGIN FETCH cv_Equipment INTO equipment_Row; END FetchItem; END ManageEquipment; 14
  • 15. /* PL SQL Example 13.16 File: ch13-16.sql */ DECLARE -- Declare a cursor variable of the REF CURSOR type item_Cursor ManageEquipment.equipment_Type; v_EquipmentNumber equipment.EquipmentNumber%TYPE; equipment_Row equipment%ROWTYPE; BEGIN -- Assign a equipment number to the variable v_EquipmentNumber := '5001'; -- Open the cursor using a variable ManageEquipment.OpenItem (item_Cursor, v_EquipmentNumber); -- Fetch the equipment data and display it LOOP ManageEquipment.FetchItem( item_Cursor, equipment_Row); EXIT WHEN item_cursor%NOTFOUND; DBMS_OUTPUT.PUT (equipment_Row.EquipmentNumber || ' '); DBMS_OUTPUT.PUT_LINE (equipment_Row.Description); END LOOP; END; 5001 Computer, Desktop PL/SQL procedure successfully completed. 15