Unit 5 Using PL SQL Package
Unit 5 Using PL SQL Package
• Stored Procedures
• PL/SQL Functions
• Database Cursors
• Type declarations as well as
• Variables
Package Architecture
It means that you can enhance the code in the body of the package without affecting
other dependent packages or applications.
• The elements which are all declared in the specification can be accessed from
outside of the package. Such elements are known as a public element.
• The package specification is a standalone element that means it can exist alone
without package body.
• Whenever a package has referred an instance of the package is created for that
particular session.
• After the instance is created for a session, all the package elements that are
initiated in that instance are valid until the end of the session.
Syntax
CREATE [OR REPLACE] PACKAGE <package_name>
IS
<sub_program and public element declaration>
.
.
END <package name>
• It should contain definitions for all the subprograms/cursors that have been
declared in the specification.
• It can also have more subprograms or other elements that are not declared in
specification. These are called private elements.
• It is a dependable object, and it depends on package specification.
• The state of the package body becomes 'Invalid' whenever the specification is
compiled. Therefore, it needs to be recompiled each time after the compilation
of specification.
• The private elements should be defined first before they are used in the
package body.
• The first part of the package is the global declaration part. This includes
variables, cursors and private elements (forward declaration) that is visible to
the entire package.
• The last part of the package is Package initialization part that executes one
time whenever a package is referred first time in the session.
Syntax
CREATE [OR REPLACE] PACKAGE BODY <package_name>
IS
<global_declaration part>
<Private element definition>
<sub_program and public element definition>
.
<Package Initialization>
END <package_name>
Once the elements are declared and defined in the package, we need to refer the
elements to use them.
All the public elements of the package can be referred by calling the package name
followed by the element name separated by period
i.e. '<package_name>.<element_name>'.
The public variable of the package can also be used in the same way to assign and
fetch values from them
i.e. '<package_name>.<variable_name>'.
As we talked while discussing the architecture of the package in the previous topic
that the package is divided into two parts, which are
Example: In this example, we are going to create a package to get and set the values
of employee's information in 'emp' table. The get_record function will return the
record type output for the given employee number, and set_record procedure will
insert the record type record into the emp table.
Output:
Package created
Step 2) Package contains Package body, where all procedures and functions actual
definition will be defined. In this step, Package Body is created.
Output:
Package body created
Step 3) Creating an anonymous block to insert and display the records by referring
to the above created package.
DECLARE
l_emp_rec emp%ROWTYPE;
l_get_rec emp%ROWTYPE;
BEGIN
dbms output.put line(‘Insert new record for employee 1004');
l_emp_rec.emp_no:=l004;
l_emp_rec.emp_name:='CCC';
l_emp_rec.salary~20000;
l_emp_rec.manager:=’BBB’;
pkg_get_set.set_record(1_emp_rec);
dbms_output.put_line(‘Record inserted');
dbms output.put line(‘Calling get function to display the inserted record'):
l_get_rec:=pkg_get_set.get_record(1004);
dbms_output.put_line(‘Employee name: ‘||l_get_rec.emp_name);
dbms_output.put_line(‘Employee number:‘||l_get_rec.emp_no);
dbms_output.put_line(‘Employee salary:‘||l_get_rec.salary');
dbms output.put line(‘Employee manager:‘||1_get_rec.manager);
END:
/
Output:
Insert new record for employee 1004
Control is now executing the package initialization part
Record inserted
Calling get function to display the inserted record
Employee name: CCC
Employee number: 1004
Employee salary: 20000
Employee manager: BBB
Example
as
end BOOK_MANAGEMENT;
Now the procedure and function here are public and accessible to the outside world.
END BOOK_MANAGEMENT;
Note above the number of books is private and isn't shown to the user. It is however
(assumed to be) necessary to implement the methods. Similarly, you could have
private functions also.
as
end BOOK_MANAGEMENT;
For example, package STANDARD declares function ABS, which returns the
absolute value of its argument, as follows:
The contents of package STANDARD are directly visible to applications. You need
not qualify references to its contents by prefixing the package name. For example,
you might invoke ABS from a database trigger, stored subprogram, Oracle tool, or
3GL application, as follows:
If you declare your own version of ABS, your local declaration overrides the global
declaration. You can still invoke the built-in function by specifying its full name: