Functions in PLSQL
Functions in PLSQL
Functions in PLSQL
NAME : Manobalan P
ROLL NO. : 22ITU088
DEPARTMENT: Computer Technology
COURSE : B.Sc(IT)-2nd Year
SUBJECT : RDBMS
SUBJECT CODE : 22ITU302
SUBMITTED TO : Mrs.S.BHUVANESWARI
PL/SQL 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.
Customers
Id Name Department Salary
1 alex web developer 35000
2 ricky program 45000
developer
3 mohan web designer 35000
4 dilshad database 44000
manager
Create Function:
1.CREATE OR REPLACE FUNCTION totalCustomers
2.RETURN number IS
3. total number(2) := 0;
4.BEGIN
5. SELECT count(*) into total
6. FROM customers;
7. RETURN total;
8.END;
9./
After the execution of above code, you will get the following result.
Function created.
Calling PL/SQL Function:
While creating a function, you have to give a definition of what the function has to do. To use a function, you will
have to call that function to perform the defined task. Once the function is called, the program control is
transferred to the called function.
After the successful completion of the defined task, the call function returns program control back to the main
program.
To call a function you have to pass the required parameters along with function name and if function returns a
value then you can store returned value. Following program calls the function totalCustomers from an
anonymous block:
1.DECLARE
2. c number(2);
3.BEGIN
4. c := totalCustomers();
5. dbms_output.put_line('Total no. of Customers: ' || c);
6.END;
7./
After the execution of above code in SQL prompt, you will get the following result.
Total no. of Customers: 4 PL/SQL procedure successfully completed.