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

Functions & Stored Procedure-1

A subprogram is a program module that performs a specific task. Subprograms can be procedures or functions. Procedures do not return a value while functions return a single value. Procedures are created using CREATE PROCEDURE while functions are created using CREATE FUNCTION. Subprograms can be invoked from other subprograms or programs and help divide programs into modular pieces to make them more readable and maintainable.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Functions & Stored Procedure-1

A subprogram is a program module that performs a specific task. Subprograms can be procedures or functions. Procedures do not return a value while functions return a single value. Procedures are created using CREATE PROCEDURE while functions are created using CREATE FUNCTION. Subprograms can be invoked from other subprograms or programs and help divide programs into modular pieces to make them more readable and maintainable.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

A subprogram is a 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.
A subprogram can be created −

 At the schema level


 Inside a package
 Inside a PL/SQL block
At the schema level, subprogram is a standalone subprogram. It is created with the
CREATE PROCEDURE or the CREATE FUNCTION statement. It is stored in the
database and can be deleted with the DROP PROCEDURE or DROP FUNCTION
statement.
A subprogram created inside a package is a packaged subprogram. It is stored in the
database and can be deleted only when the package is deleted with the DROP
PACKAGE statement..
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of
parameters. PL/SQL provides two kinds of subprograms −
 Functions − These subprograms return a single value; mainly used to compute
and return a value.
 Procedures − These subprograms do not return a value directly; mainly used to
perform an action.
PROCEDURE
Give difference between stored procedure and PL/SQL function

Creating a Procedure

A procedure is created with the CREATE OR REPLACE PROCEDURE statement.


The simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as
follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;

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:

Executing a Standalone Procedure

A standalone procedure can be called in two ways −


 Using the EXECUTE keyword
 Calling the name of the procedure from a PL/SQL block
The above procedure named ‘myproc’ can be called with the EXECUTE keyword as
EXECUTE myproc;
Output:
Government Polytechnic Gondia

Statement processed.
The procedure can also be called from another PL/SQL block −
BEGIN
myproc;
END;
Output:
Government Polytechnic Gondia

Statement processed.

Deleting a Standalone Procedure

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

A standalone function is created using the CREATE FUNCTION statement. The


simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as
follows −
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Where,
 function-name specifies the name of the function.
 [OR REPLACE] option allows the modification of an existing function.
 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.
 The function must contain a return statement.
 The RETURN clause specifies the data type you are going to return from the
function.
 function-body contains the executable part.
 The AS keyword is used instead of the IS keyword for creating a standalone
function.
Example
The following example illustrates how to create and call a standalone function. This
function returns the total number of CUSTOMERS in the customerstab table.
Select * from customerstab;

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

CREATE OR REPLACE FUNCTION totalCustomers


RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customerstab;

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;

FUNCTION fact(x number)


RETURN number
IS
f number;
BEGIN
IF x=0 THEN
f := 1;
ELSE
f := x * fact(x-1);
END IF;
RETURN f;
END;

BEGIN
num:= 5;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' ||
factorial);
END;
/
Output:
Factorial 5 is 120

Statement processed.

You might also like