Professional Documents
Culture Documents
(PL/SQL object)
PROCEDURES/FUNCTIONS
Procedurename [(parameter1,…..)];
For example :
p_sum(20,30);
Calling a procedure
Also, procedure can be called using
EXECUTE (EXEC) command:
For Example :
EXEC p_sum(20,30);
SQL> create or replace procedure p1
(n1 in number,n2 in number,n3 out
number)
2 is PERAMETERS
PROCEDURE
3 begin HEAD
4 n3:=n1+n2;
5 dbms_output.put_line('sum is = '||n3);
6 end; PROCEDURE
7 / BODY
Procedure created.
CALLING A PROCEDURE
Set serveroutput on
SQL> declare
2 n3 number;
3 begin
4 p1(10,20,n3);
5 end; PROCEDURE
6 / CALL
SUM IS = 50
declare
2 a number:=&a;
3 b number:=&b;
4 n3 number;
5 begin
6 p1(a,b,n3);
7 end;
8 /
Enter value for a: 30
old 2: a number:=&a;
new 2: a number:=30;
Enter value for b: 70
old 3: b number:=&b;
new 3: b number:=70;
sum is = 100
Syntax :
CREATE OR REPLACE FUNCTION <Fun
Name>
(<Parameter List>) RETURN <Data Type>
{is/as}
<declaration section>
BEGIN
<PL/SQL Code>
RETURN <expression>
EXCEPTION section
<PL/SQL Code>
END;
The main difference between a function
and a procedure is that a function always
returns a value to the calling block.
Primary use of a Function is to return a
value with an explicit RETURN statement.
The body can contain more than one
RETURN statement, but only one is
executed with each function call.
Example:-
SQL> create or replace FUNCTION f1 (n1 in
number,n2 in number) return number
2 is
3 n3 number; parameter Function
header
4 begin
5 n3:=n1+n2; Local variable
declaration
6 return n3;
7 end; Function
8 / body
Function created.
Set serveroutput on
SQL> declare
2 x number:=&x;
3 y number:=&y;
4 z number; Function
5 begin
call
6 z:=f1(x,y);
7 dbms_output.put_line('total is ='||z);
8 end;
9 /
Enter value for x: 10
old 2: x number:=&x;
new 2: x number:=10;
Enter value for y: 20
old 3: y number:=&y;
new 3: y number:=20;
DROP PROCEDURE
<Procedure_name>;
DROP FUNCTION
<Function_name>;