Package :
Is a schema object that logically groups related pl/sql types,variables and subprograms.
Subprograms defined in package spec becomes public.
Subprograms defined in package body becomes private, are accessible to package members only.
See below package - XX_EMP_DETAILS
It has a procedure define in spec - DISPLAY_EMP_DETAILS
It is a public object. It can be referenced from outside the package like
XX_EMP_DETAILS.DISPLAY_EMP_DETAILS
If we see package body, a function get_yearly_salary is defined.
It becomes private.
It can't be accessed from outside the package.
See the code details below :
CREATE OR REPLACE PACKAGE XX_EMP_DETAILS
AS
--Public Methods
PROCEDURE DISPLAY_EMP_DETAILS(p_id employees.employee_id%type);
END XX_EMP_DETAILS;
/
CREATE OR REPLACE PACKAGE BODY XX_EMP_DETAILS
AS
--Defining a private method
FUNCTION GET_YEARLY_SALARY(p_id employees.employee_id%type )
RETURN NUMBER
AS
mon_sal number;
BEGIN
select salary*12
into mon_sal
from employees
where employee_id=p_id;
return mon_sal;
end get_yearly_salary;
PROCEDURE DISPLAY_EMP_DETAILS(p_id employees.employee_id%type)
IS
v_first_name varchar2(100);
v_last_name varchar2(100);
v_sal number;
v_dept employees.department_id%type;
BEGIN
SELECT
first_name,
last_name,
department_id
into
v_first_name,
v_last_name,
v_dept
from employees
where employee_id = p_id;
--get yearly salary by calling private function get_yearly_salary
v_sal:=get_yearly_salary(p_id);
DBMS_OUTPUT.PUT_LINE('Employee Details : ');
DBMS_OUTPUT.PUT_LINE(rpad('FIRST NAME',20)||' '||rpad('LAST NAME',20)||' '
||Lpad('YEARLY SALARY',10)||' '||Lpad('DEPARTMENT',5));
DBMS_OUTPUT.PUT_LINE(rpad(v_first_name,20)||' '||rpad(v_last_name,20)||' '
||Lpad(v_sal,10)||' '||Lpad(v_dept,5));
DBMS_OUTPUT.PUT_LINE('END of Employee Details');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee Does not exist');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END DISPLAY_EMP_DETAILS;
end XX_EMP_DETAILS;
/
Now call public procedure and see. Here private function is
accessed in the procedure.
begin
XX_EMP_DETAILS.DISPLAY_EMP_DETAILS(100);
end;
/
Now call private function and check.
It throws an error
If we see procedure call, as it is a package member, it can access private subprograms.
Is a schema object that logically groups related pl/sql types,variables and subprograms.
Subprograms defined in package spec becomes public.
Subprograms defined in package body becomes private, are accessible to package members only.
See below package - XX_EMP_DETAILS
It has a procedure define in spec - DISPLAY_EMP_DETAILS
It is a public object. It can be referenced from outside the package like
XX_EMP_DETAILS.DISPLAY_EMP_DETAILS
If we see package body, a function get_yearly_salary is defined.
It becomes private.
It can't be accessed from outside the package.
See the code details below :
CREATE OR REPLACE PACKAGE XX_EMP_DETAILS
AS
--Public Methods
PROCEDURE DISPLAY_EMP_DETAILS(p_id employees.employee_id%type);
END XX_EMP_DETAILS;
/
CREATE OR REPLACE PACKAGE BODY XX_EMP_DETAILS
AS
--Defining a private method
FUNCTION GET_YEARLY_SALARY(p_id employees.employee_id%type )
RETURN NUMBER
AS
mon_sal number;
BEGIN
select salary*12
into mon_sal
from employees
where employee_id=p_id;
return mon_sal;
end get_yearly_salary;
PROCEDURE DISPLAY_EMP_DETAILS(p_id employees.employee_id%type)
IS
v_first_name varchar2(100);
v_last_name varchar2(100);
v_sal number;
v_dept employees.department_id%type;
BEGIN
SELECT
first_name,
last_name,
department_id
into
v_first_name,
v_last_name,
v_dept
from employees
where employee_id = p_id;
--get yearly salary by calling private function get_yearly_salary
v_sal:=get_yearly_salary(p_id);
DBMS_OUTPUT.PUT_LINE('Employee Details : ');
DBMS_OUTPUT.PUT_LINE(rpad('FIRST NAME',20)||' '||rpad('LAST NAME',20)||' '
||Lpad('YEARLY SALARY',10)||' '||Lpad('DEPARTMENT',5));
DBMS_OUTPUT.PUT_LINE(rpad(v_first_name,20)||' '||rpad(v_last_name,20)||' '
||Lpad(v_sal,10)||' '||Lpad(v_dept,5));
DBMS_OUTPUT.PUT_LINE('END of Employee Details');
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee Does not exist');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END DISPLAY_EMP_DETAILS;
end XX_EMP_DETAILS;
/
Now call public procedure and see. Here private function is
accessed in the procedure.
begin
XX_EMP_DETAILS.DISPLAY_EMP_DETAILS(100);
end;
/
Now call private function and check.
It throws an error
If we see procedure call, as it is a package member, it can access private subprograms.
Hello Nag,
ReplyDelete1)can we declare a function as public object?
2)can a function have out variables apart from return value?
Thanks,Santosh
Hi Santosh,
ReplyDelete1) We create a function, then we need to make it public then create a public synonym for that function. Then grant all privileges on the synonym to other schemas.
2) Yes, we can have OUT, IN OUT parameters in a function definition.
Thanks,
Nagarjuna