Sub topic 4
Creating a Stored Function and Returning a Value
This chapter assumes that you know basic SQL and anonymous PL/SQL blocks. It also assumes access to an employees table with employee_id, first_name, and last_name columns.
A stored function is a named PL/SQL program that returns one value. Use a function when the main result of an operation is data that another block or SQL statement can use. A procedure is usually better when the operation performs an action, such as updating rows.
Function structure
A function declaration includes:
- A name and optional parameters.
- The
RETURNkeyword followed by the return data type. - A body containing declarations and executable statements.
- A
RETURNstatement that supplies the result.
CREATE OR REPLACE creates the function if it does not exist or replaces its current definition.
CREATE OR REPLACE FUNCTION get_full_name (
p_employee_id IN employees.employee_id%TYPE
) RETURN VARCHAR2 AS
v_name VARCHAR2(100);
BEGIN
SELECT first_name || ' ' || last_name
INTO v_name
FROM employees
WHERE employee_id = p_employee_id;
RETURN v_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END get_full_name;
/The parameter uses %TYPE, an anchored type. This makes p_employee_id use the same data type as employees.employee_id. If the column type changes later, the function can be recompiled with the matching type.
The SELECT INTO statement must assign exactly one row to v_name. The function then returns that variable. When no employee matches, Oracle raises the predefined NO_DATA_FOUND exception. This function handles that case by returning NULL.
A function must return a value on every successful execution path. Forgetting a RETURN statement causes a compilation error or a runtime error, depending on the path taken.
Calling the function
Call a function from an anonymous block by assigning its result to a variable:
SET SERVEROUTPUT ON
DECLARE
v_name VARCHAR2(100);
BEGIN
v_name := get_full_name(100);
DBMS_OUTPUT.PUT_LINE('Employee: ' || NVL(v_name, 'unknown'));
END;
/You can also call a stored function in a SQL expression:
SELECT employee_id,
get_full_name(employee_id) AS full_name
FROM employees
WHERE department_id = 60;Oracle evaluates the function for each row returned by the query. This is convenient, but a function that performs expensive work can make a query slow.
Common errors
| Error | Cause | Typical fix |
|---|---|---|
| `ORA-01403` | No row matched `SELECT INTO` | Handle `NO_DATA_FOUND` or allow it to propagate |
| `ORA-01422` | More than one row matched | Make the filter unique or aggregate the rows |
| Missing return value | An execution path reaches the end | Add a `RETURN` statement |
| Invalid identifier | A column or table name is wrong | Check the referenced schema objects |
A function can raise an error deliberately with RAISE_APPLICATION_ERROR when returning a value would hide a serious problem. Choose a clear contract: return NULL for an expected missing value, or raise an exception when the missing value indicates invalid input.
Exercise
What does RETURN VARCHAR2 specify in the function declaration?
Show answer
It specifies that the function must return one value whose data type is VARCHAR2.