Sub topic 9
Calling Stored Functions from SQL
This chapter assumes basic SQL, PL/SQL blocks, Oracle data types, and the ability to create stored procedures and functions.
A stored function can be called from a SQL statement when it returns a value that SQL can use. The function must have parameters that SQL can supply, and the returned value must have a SQL-compatible type. The most common use is to calculate or format a value for each row.
Calling a function in a query
The following function returns an employee's full name. It returns NULL when the employee does not exist.
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;
/You can call the function in the select list:
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. If the query returns 20 rows, the function may run 20 times. This row-by-row execution can be slower than an equivalent join, especially for large result sets.
Restrictions and side effects
A function used in SQL should behave like a calculation. Avoid changing database state while SQL is reading or modifying data.
Common restrictions include:
- A function called from SQL cannot normally execute transaction-control statements such as
COMMITorROLLBACK. - A function called from a query cannot normally perform DML such as
INSERT,UPDATE, orDELETE. Oracle raises an error such asORA-14551. - A function used by a query should not update the same table that the query is reading. This can produce a mutating-table error.
- SQL calls should use functions with
INparameters.OUTandIN OUTparameters are not suitable for ordinary SQL expressions. - Exceptions that escape the function cause the SQL statement to fail. Handle expected cases inside the function or let the caller handle the failure.
An autonomous transaction can allow a function to write independently, but it changes transaction behavior and should be reserved for carefully designed cases such as logging. It does not make side effects harmless.
> callout warning > Do not add DML to a query function merely to record debugging information. Use deliberate logging design, and understand its transaction and performance effects first. > ```
Choosing the implementation
A join or built-in SQL expression is often faster than calling a PL/SQL function for every row. Use a stored function when it centralizes reusable business logic, improves readability, or hides complicated rules. Test the query with realistic row counts before relying on it in a high-volume report.
A function's declaration does not prove that it has no side effects. DETERMINISTIC is a promise about repeated inputs and outputs, not an automatic safety check.
Exercise
What happens if getfullname raises an unhandled exception while processing one row of a SELECT statement?
Show answer
The SQL statement fails, unless the function handles that exception or the SQL expression handles the resulting value.