You are on page 1of 10

EXP NO :

PROCEDURES AND FUNCTIONS


DATE :

AIM :

To create a database and apply Procedures and Functions

PL/SQL subprograms:

A subprogram is a named block of PL/SQL. There are two types of subprograms in


PL/SQL namely

• Procedures
• Functions

Every subprogram will have a declarative part, an executable part or body, and an
exception handling part, which is optional.

Declarative part contains variable declarations. Body of a subprogram contains


executable statements of SQL and PL/SQL. Statements to handle exceptions are written in
exception part.

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.

Parameter Modes in PL/SQL Subprograms

• 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:

CREATE [OR REPLACE] PROCEDURE procedure_name


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;

Consider the customer table having the following records


PROCEDURE FOR INSERTING:

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 update a record in the customer table .The following


code create the procedure named updatecustomer.
PROCEDURE FOR DELETING 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:

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.
FUNCTION PROGRAM 1:
Consider the customer table having the following records

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 :

Database created successfully. Procedures and Functions are executed successfully.

You might also like