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

UNIT-3_Function

The document provides an overview of PL/SQL functions, highlighting their purpose, structure, and syntax. Functions are similar to procedures but must return a value and can include parameters for input and output. It also includes examples of creating and calling a function, as well as how to drop a function from the database.

Uploaded by

gamerxyz756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

UNIT-3_Function

The document provides an overview of PL/SQL functions, highlighting their purpose, structure, and syntax. Functions are similar to procedures but must return a value and can include parameters for input and output. It also includes examples of creating and calling a function, as well as how to drop a function from the database.

Uploaded by

gamerxyz756
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

iMSCIT SEM-V

DATABASE SYSTEM-II
Unit:3

Stored Procedures and Functions


PL/SQL Functions

A function is same as a procedure except that it returns a value.

Functions are a standalone block that is mainly used for
calculation purpose.

Function use RETURN keyword to return the value, and the
datatype of this is defined at the time of creation.

A Function should either return a value or raise the exception, i.e.
return is mandatory in functions.

It can have nested blocks, or it can be defined and nested inside
the other blocks or packages.
PL/SQL Functions

It contains declaration part (optional), execution part,
exception handling part (optional).

The values can be passed into the function or fetched from the
procedure through the parameters.

These parameters should be included in the calling statement.

Since it will always return the value, in calling statement it
always accompanies with assignment operator to populate the
variables.
Syntax to create a function:

CREATE [OR REPLACE] FUNCTION function_name

[(parameter_name [IN | OUT | IN OUT] type [, ...])]

RETURN return_datatype

{IS | AS}

BEGIN

< function_body >

END [function_name];
Syntax to create a function:

Function_name: specifies the name of the function.

[OR REPLACE] option allows modifying an existing function.

The optional parameter list contains name, mode and types of the
parameters.

IN represents that value will be passed from outside and OUT
represents that this parameter will be used to return a value outside of
the procedure.
Syntax to create a function:

The function must contain a return statement.



RETURN clause specifies that 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 of Function:
create or replace function adder(n1 in number, n2 in number)

return number

is

n3 number(8);

begin

n3 :=n1+n2;

return n3;

end;

/
Calling a Fuction:
DECLARE

n3 number(2);

BEGIN

n3 := adder(11,22);

dbms_output.put_line('Addition is: ' || n3);

END;

/
PL/SQL Drop Function

If you want to remove your created function from the database, you
should use the following syntax.

DROP FUNCTION function_name;

You might also like