Functions & Stored Procedure-1
Functions & Stored Procedure-1
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.
A subprogram can be created −
Creating a Procedure
Where,
procedure-name specifies the name of the procedure.
[OR REPLACE] option allows the modification of an existing procedure.
The optional parameter list contains name, mode and types of the
parameters. IN represents the value that will be passed from outside and OUT
represents the parameter that will be used to return a value outside of the
procedure.
procedure-body contains the executable part.
The AS keyword is used instead of the IS keyword for creating a standalone
procedure.
CREATE OR REPLACE PROCEDURE myproc
AS
BEGIN
dbms_output.put_line('Government Polytechnic Gondia');
END;
Output:
Statement processed.
The procedure can also be called from another PL/SQL block −
BEGIN
myproc;
END;
Output:
Government Polytechnic Gondia
Statement processed.
A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for
deleting a procedure is −
DROP PROCEDURE procedure-name;
You can drop the myproc procedure by using the following statement −
DROP PROCEDURE myproc;
Example
Above Code:
CREATE OR REPLACE PROCEDURE cust_details(cust_id NUMBER)
IS
cust customers%ROWTYPE;
BEGIN
SELECT * INTO cust FROM customers WHERE id = cust_id;
dbms_output.put_line( cust.name || ' ' || cust.age || ' ' || cust.salary ||' ' );
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line( 'Error' );
END;
**************
Practice Code:
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;
Output:
Minimum of (23, 45) : 23
Statement processed.
Practice Code:
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
Output:
Square of (23): 529
Statement processed.
FUNCTIONS
Creating a Function
Output:
ID NAME AGE ADDRESS SAL ARY
1 Ramesh 32 Ahmedabad 2500
2 Khilan 25 Delhi 2000
3 kaushik 23 Kota 2500
4 Chaitali 25 Mumbai 7000
5 Hardik 27 Bhopal 9000
6 Komal 22 MP 5000
RETURN total;
END;
Output:
Calling a Function
When a program calls a function, the program control is transferred to the called
function.
A called function performs the defined task and when its return statement is executed
or when the last end statement is reached, it returns the program control back to the
main program.
To call a function, you simply need to pass the required parameters along with the
function name and if the function returns a value, then you can store the returned
value. Following program calls the function totalCustomers from an anonymous block.
DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
Output:
Total no. of Customers: 6
Statement processed.
Practice code:
The following example demonstrates Declaring, Defining, and Invoking a Simple
PL/SQL Function that computes and returns the maximum of two values.
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 15;
b:= 7;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (15,7): ' || c);
END;
Output:
Maximum of (15,7): 15
Statement processed.
Practice Code:
The following program calculates the factorial of a given number by calling itself
recursively −
DECLARE
num number;
factorial number;
BEGIN
num:= 5;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' ||
factorial);
END;
/
Output:
Factorial 5 is 120
Statement processed.