Expno: Procedures and Functions Date
Expno: Procedures and Functions Date
Expno: Procedures and Functions Date
AIM :
PL/SQL subprograms:
• Procedures
• Functions
Every subprogram will have a declarative part, an executable part or body, and an
exception handling part, which is optional.
When client executes a procedure are function, the processing is done in the server.
This reduces network traffic. The subprograms are compiled and stored in the Oracle
database as stored programs and can be invoked whenever required. As they are stored in
compiled form when called they only need to be executed. Hence they save time needed for
compilation.
• IN - The parameter can be referenced by the procedure or function. The value of the
parameter can not be overwritten by the procedure or function.
• OUT - The parameter can not be referenced by the procedure or function, but the
value of the parameter can be overwritten by the procedure or function.
• IN OUT - The parameter can be referenced by the procedure or function and the value
of the parameter can be overwritten by the procedure or function.
PROCEDURE:
A procedure is a named PL/SQL block which perform one or more specific task.
It do not return a value directly; mainly used to perform an action. They can be used for
validation, access control, or to reduce network traffic between client and DBMS server .
A procedure is created with the CREATE OR REPLACE PROCEDURE statement.
SYNTAX:
Suppose we want to insert a record in the customer table .The following code
create the procedure named insertcustomer .
PROCEDURE FOR UPDATING VALUES :
Suppose we want to delete a record in the customer table .The following code
create the procedure named deletecustomer.
PROCEDURE FOR SELECTING:
Suppose we want to select a record in the customer table .The following code
create the procedure named selectcustomer.
FUNCTION:
The function is a set of SQL statements that perform a specific task. Functions
foster code reusability. If you have to repeatedly write large SQL scripts to perform the same
task, you can create a function that performs that task. Next time instead of rewriting the
SQL, you can simply call that function. A function accepts inputs in the form of parameters
and returns a value.
SYNTAX:
Where,
Now, we will create a function that returns the total number of customers in
the table
FUNCTION PROGRAM 2:
Consider the stmark table having the following records
Now, we will create a function that returns the average mark of student in the table
RESULT :