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

Chap-4 Procedure and Function

Uploaded by

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

Chap-4 Procedure and Function

Uploaded by

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

Procedure

• Procedures in PL/SQL is subprogram program


unit/module that performs a particular task.
These subprograms are combined to form
larger programs.
• This is basically called the 'Modular design'. A
subprogram can be invoked by another
subprogram or program which is called
the calling program.
Advantages of procedure
• Better Performance – The procedure calls are quick and efficient as stored procedures are

compiled once and stored in executable form. Hence the response is quick. The

executable code is automatically cached, hence lowers the memory requirements.

• 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.

• Scalability – Stored procedures increase scalability by isolating application processing on

the server.

• Maintainability – Maintaining a procedure on a server is much easier then maintaining

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;

Two Main parts of procedure are specification and body.


Procedure specification begins with the keyword is and end with
keyword 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

create or replace procedure chkin(x in number,y out number)is


begin
y:=x*x;
end

declare
res number;
x number:=10;
begin
chkin(x,res);
dbms_output.put_line(res);
end;
Function

The PL/SQL Function is very similar to PL/SQL Procedure.


The main difference between procedure and a function is, a
function must always return a value, and on the other hand a
procedure may or may not return a value. Except this, all the
other things of PL/SQL procedure are true for PL/SQL function
too.
Function Syntax
Create or replace function functionname[argument]
return datatype is
Variable declaration;
BEGIN
(Executable statements)
[Exception](Exception Handler)
End;
Function parameter and return type

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.

Return type- Function can return value using return


Example function with in parameter
create or replace function sqr1(x in number) return number is
begin
return x*x;
end;

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

You might also like