0% found this document useful (0 votes)
90 views

PLSQL Lecture-6

This document discusses creating and using packages in Oracle PL/SQL. It shows how to create a package specification with a function and procedure, create the corresponding package body, and call the function and procedure. The function checks if an employee record exists, and the procedure calculates and displays the employee's net pay if the record is found.

Uploaded by

cavad565
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

PLSQL Lecture-6

This document discusses creating and using packages in Oracle PL/SQL. It shows how to create a package specification with a function and procedure, create the corresponding package body, and call the function and procedure. The function checks if an employee record exists, and the procedure calculates and displays the employee's net pay if the record is found.

Uploaded by

cavad565
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Advance Databases

CREATING PACKAGES

[email protected]

PLSQL Tip
Error: Output buffer overflow
Solution: set the buffer size to a larger value

How

DBMS_OUTPUT is useful for debugging PL/SQL


programs. However, if you print too much, the output
buffer will overflow. In that case, set the buffer size to
a larger value, eg.:
SQL> set serveroutput on size 20000

PACKAGES

Packages has two parts

1- Package Specification
2- Package Body

CREATE A PACKAGE
A package is to be created that will contain a function
and a procedure.

Procedure will take empno as an input and it will


show NET_PAY [sal + comm] of that employee.

Where as Function namely FIND_EMP will confirm


that the record of a particular employee exists or not
before executing NET_PAY procedure.

Creating Package
Specification

CREATE OR REPLACE PACKAGE emp_package


is
PROCEDURE NET_PAY (v_empno in number);
FUNCTION FIND_EMP (v_empno in number)
RETURN BOOLEAN;
END EMP_PACKAGE;

PROCEDURE to
calculate NET_SAL
This Procedure will take empno as an input
and it will show NET PAY [sal + comm]
comm] of that employee.

create or replace PROCEDURE net_sal


(v_empno in number)
is
v_temp_sal number;
begin
select sal + comm into v_temp_sal from emp
where empno = v_empno;
v_empno;
DBMS_OUTPUT. PUT_LINE ('Net salary of ' || v_empno ||
' is ' || v_temp_sal);
v_temp_sal);
END;

SQL> execute net_sal(7499);


Net salary of 7499 is 1900
PL/SQL procedure successfully completed.

FUNCTION FIND_EMP
Function namely FIND_EMP will confirm
that the record of a particular employee exists or not
before executing NET PAY procedure.

FUNCTION FIND_EMP
create or replace Function find_emp (v_empno in number)
return boolean
is
v_temp number;
begin
select empno into v_temp from emp
where empno = v_empno;
Oracle includes about 20
predefined exceptions (errors) return(TRUE);
we can allow Oracle to raise
exception
these implicitly.
when no_data_found then
when too_many_rows then
return(FALSE);
when no_data_found then
when others then
END;
See more details on web/Book

CALLING FIND_EMP
FUNCTION
FROM NET_SAL
PROCEDURE

create or replace PROCEDURE net_sal


(v_empno in number)
is
v_temp_sal number;
begin
if (find_emp(v_empno
)) then
(find_emp(v_empno))
select sal + comm into v_temp_sal from emp
where empno = v_empno;
v_empno;
DBMS_OUTPUT. PUT_LINE
('Net salary of ' || v_empno || ' is ' || v_temp_sal);
v_temp_sal);
else

DBMS_OUTPUT. PUT_LINE
('Record not found of Employee ' || v_empno || '.');

end if;
END;

How to use both Function


and Procedure in one
Package body
create or replace package emp_package is
procedure net_pay (v_empno in number);
function find_emp (v_empno in number)
return boolean;
end emp_package;

CREATE OR REPLACE PACKAGE BODY EMP_PACKAGE IS


FUNCTION FIND_EMP (v_empno
(v_empno in number) RETURN BOOLEAN
is
v_temp number;
begin
select empno into v_temp from emp where empno = v_empno;
v_empno;
return(TRUE);
return(TRUE);
EXCEPTION
when no_data_found then return(FALSE);
return(FALSE);
END;
PROCEDURE net_pay (v_empno in number)
is
v_temp_sal number;
begin
if (find_emp(v_empno
)) then
(find_emp(v_empno))
select sal + comm into v_temp_sal from emp where empno =
v_empno;
v_empno;
DBMS_OUTPUT. PUT_LINE ('Net salary of ' || v_empno || ' is ' ||
v_temp_sal);
v_temp_sal);
else
DBMS_OUTPUT. PUT_LINE ('Record not found of Employee ' || v_empno || '.');

end if; END;

end emp_package;
emp_package;

Execution of Package FUNCTION


and
Package PROCEDURE

SQL> EXECUTE emp_package.


emp_package. net_pay(7499);
Net salary of 7499 is 1900
PL/SQL procedure successfully completed.

SQL> EXECUTE emp_package. net_pay(736);


Record not found of Employee 736.
PL/SQL procedure successfully completed.

Thanks
Q&A
Next PLSQL Triggers

You might also like