User-Defined Functions «Prev 

Primary Advantage of Functions in Oracle

What is a function in PL/SQL?

The primary advantage of a function is to let you, as the designer or developer, create an encapsulated set of code that might otherwise be impossible to accomplish within an SQL command. For example, you might use a function to handle a calculation that varies depending on some related information (such as calculating a long distance phone call charge based on the appropriate daytime, weekend, or evening rate). It would be challenging to attempt this using a SQL Query.

A function is a subprogram that computes a value. Functions and procedures are structured alike, except that functions have a RETURN clause. You write (local) functions using the syntax: A stored function is similar to a stored procedure with one key difference:
A stored function returns a single value.
This makes a stored function available in your SQL SELECT statements, unlike stored procedures that you must call within an anonymous PL/SQL block or another stored procedure.


Although you might argue that Oracle already offers more than enough functions, you may find that you need a specific capability that is not already provided. In that case, you can develop your own functions (using PL/SQL) and add them to the SQL language. PL/SQL is the standard procedural programming language for Oracle databases. PL/SQL is a superset of SQL, adding several procedural capabilities to the nonprocedural SQL language. Here, we will investigate one simple example of PL/SQL language usage in relation to custom functions. For more information about PL/SQL, refer to the User Guide and Reference of Oracle PL/SQL . The listing below shows how to define a function to determine the number of employees for a given department.

Creating a Stored Function Using PL/SQL

create or replace function emp_count(p_deptno in number)
return number
is
cnt number(2) := 0;
begin
select count(*)
into cnt
from employees e
where e.deptno = p_deptno;
return (cnt);
end;

Advantages of using functions in Oracle PL/SQL

  1. Reusability: Functions can be reused in multiple places, which can save time and effort.
  2. Efficiency: Functions can be compiled and stored in memory, which can improve performance.
  3. Maintainability: Functions can be easily modified or updated without affecting other parts of the code.
  4. Security: Functions can be restricted to certain users or roles, which can help to protect data.
  5. Portability: Functions can be easily moved to other databases, which can make it easier to develop and maintain applications.

Here are some examples of how functions can be used in Oracle PL/SQL:
  1. A function can be used to validate data before it is entered into the database.
  2. A function can be used to calculate a value based on other data in the database.
  3. A function can be used to perform a complex operation that would be difficult to code in SQL.
  4. A function can be used to encapsulate a piece of code that is used in multiple places, which can make the code easier to understand and maintain.
  5. Overall, functions can be a powerful tool for improving the performance, maintainability, and security of Oracle PL/SQL applications.