Email Us : [email protected]
Free Training > SQL PL / SQL Forms Reports Oracle Applications
< Previous Next >

 
PL / SQL Packages

cPackages are logically related sub programs.

Package creating involves two steps.

Step 1: Creating Package specification (PKS )
Step 2: Creating Package Body ( PKB )

 

Package Specification:
-----------------------------

It contains declaration of sub programs

Syntax:
----------

create or replace package <package_name>
is
declaration of procedures;
declaration of functions;

end;
/

 

Package Body:
-----------------------------

It contains definition of sub programs

Syntax:
----------

create or replace package body <package_name>
is
definition of procedures;
definition of functions;
end;
/

 

Ex:
----
Lets create a package with two procedures and function.

Procedure add_num -- which takes two parameters and display its sum.
Procedure display_emp -- which accepts empno and display ename and sal.
Function cal_tax -- which accepts sal and returns tax value (10% of sal is tax value ).

Package Specification:
---------------------------
create or replace package test_pack
is
procedure add_num ( a number,
b number);

procedure display_emp ( l_empno emp.empno%type);

function cal_tax ( l_sal emp.sal%type)
return number;

end test_pack;
/

Package body:
--------------------------

create or replace package body test_pack
is
procedure add_num ( a number,
b number)
is
c number;
begin

c := a+b;
dbms_output.put_line('The sum is '||c);
end;

procedure display_emp (l_empno emp.empno%type)
is
l_ename emp.ename%type;
l_sal emp.sal%type;

begin

select sal into l_sal from emp
where empno = l_empno;

dbms_output.put_line(l_ename||'.......'||l_sal);
end;

function cal_tax ( l_sal emp.sal%type)
is
l_tax number;
begin

l_tax := l_sal *10/100;
return l_tax;

end;

end test_pack;
/

To invoke sub programs inside the package:
--------------------------------------------

SQL> EXEC test_pack.display_emp (7900)

SQL> select empno, ename, sal, test_pack.cal_tax (sal) from emp;

Procedure overloading using packages:
---------------------------------------

We can achieve procedure overloading using Packages.
Basing on the no of parameters and datatype of the parameters,
the appropriate procedure is invoked.

ex:
----

Create or replace package test_pack2
is
procedure p1 ( a number,
b number);

procedure p1 ( a number);

end test_pack2;
/

create or replace package body test_pack2
is
procedure p1 ( a number,
b number)
is
c number;
begin
c := a+b;
dbms_output.put_line('The sum is'||c);
end;

 

procedure p1 ( a number)
is
begin

dbms_output.put_line('The square of the number is '||a*a);
end;

end test_pack2;
/

In the above package there are two procedures with the same name.

Appropriate procedure is invoked basing on the no of parameters which are passed
at the time of calling the procedure.

Ex:
--

SQL> exec test_pack2(10, 20);

The sum is 30

SQL> exec test_pack2(10);

The square of the number is 100

 

To drop the package:
----------------------------

We need to drop package bodu first and then the package specification.

Drop package body <package_name>;

Drop package <package_name>;

Ex:
------

Drop package body test_pack2;

Drop package test_pack2;

Guidelines of the packages:
----------------------------
1) Helps in modularity of the code.
2) Packages cannot be nested.
3) Packages cannot be parameterized.


< Previous Next >