Friday, 25 April 2014

Package in Oracle

Steps

1.
create table customer99999(id int, name char(20), age int, address char(20), salary int)

 2.
Insert into customer99999 values(1,'Raj',23,'Jalandhar',20000)
Insert into customer99999 values(2,'Raju',24,'Salempur',50000)
Insert into customer99999 values(3,'Ram',25,'Simraungadh',60000)
Insert into customer99999 values(4,'Raman',26,'Ludhina',70000)
Insert into customer99999 values(5,'Raju',27,'Simra',80000)

select * from customer99999
ID
NAME
AGE
ADDRESS
SALARY
1
Raj
23
Jalandhar
20000
2
Raju
24
Salempur
50000
3
Ram
25
Simraungadh
60000
4
Raman
26
Ludhina
70000
5
Raju
27
Simra
80000

How to create Package
create package customer5_salary
as
procedure find_salary(customer_id customer99999.id%type);
end customer5_salary;

Results
Package created.

How to crate package body
create or replace package body customer5_salary
as
procedure find_salary(customer_id customer99999.id%type)
is
customer_salary customer99999.salary%type;
begin
select salary into customer_salary
from customer99999
where id = customer_id;
dbms_output.put_line('Salary:'||customer_salary);
end find_salary;
end customer5_salary;

Results
Package body created.


Declare Value
set serveroutput on
declare
code customer99999.id%type:=&customer1_id;
begin
customer5_salary.find_salary(code);
end;

Results
old 2: code customer99999.id%type:= &cc_id; 
new 2: code customer99999.id%type:= 3; 
Salary:60000 
PL/SQL procedure successfully completed

2 comments: