Chap-4 Procedure and Function
Chap-4 Procedure and Function
compiled once and stored in executable form. Hence the response is quick. The
• Higher Productivity – Since the same piece of code is used again and again so, it results in
higher productivity.
• Ease of Use – To create a stored procedure, one can use any Java Integrated Development
Environment (IDE). Then, they can be deployed on any tier of network architecture.
the server.
copies on various client machines, this is because scripts are in one location.
• Security – Access to the Oracle data can be restricted by allowing users to manipulate the
data only through stored procedures that execute with their definer’s privileges.
Procedure Syntax
Create or replace procedure_name [parameter list] is
BEGIN
(Executable statements)
[Exception](Exception Handler)
End;
To execute procedure
<procedurename>(parameter list);
Procedure Parameter
1. In Parameter –
It is used to send or input values to the stored
procedure.
2. Out parameter-
It is used to output values from stored procedure.
3. In Out Parameter –
It is used to Input and output values from stored
procedure.
Sample program Parameter IN
Procedure creation -
create or replace procedure chkin(x in number)is
begin
dbms_output.put_line(x);
end
Procedure Call
declare
x number:=2;
begin
chkin(x);
end;
Sample program Parameter IN
create or replace procedure chkin(x out number)is
begin
x:=10;
End
declare
x number;
begin
chkin(x);
dbms_output.put_line(x);
end;
Sample Program Parameter IN and OUT
declare
res number;
x number:=10;
begin
chkin(x,res);
dbms_output.put_line(res);
end;
Function
declare
a number;
begin
a:=sqr1(10);
dbms_output.put_line(a);
end
Example function with out parameter
create or replace function sqr1(x out number) return number is
begin
x:=10;
return x*x;
end;
declare
a number;
ans number;
begin
ans:=sqr1(a);
dbms_output.put_line('value'||a||' square='||ans);
end