Sub topic 2
Procedures and Functions: What They Are and When to Use Each
Assume you know basic SQL and anonymous PL/SQL blocks. You should also know Oracle types such as NUMBER, VARCHAR2, and DATE.
A subprogram is a named PL/SQL block stored in the database. You can call it from an application, an anonymous block, another subprogram, or, for many functions, a SQL statement. Storing logic in one place avoids copying the same code into multiple clients.
PL/SQL has two main subprogram types:
| Subprogram | Main purpose | Returns a value? | Typical use |
|---|---|---|---|
| Procedure | Perform an action | Not as its direct result | Update data or perform a process |
| Function | Calculate or retrieve a value | Yes, with `RETURN` | Compute a total or look up a value |
Procedures perform actions
Use a procedure when the important result is an action. The procedure might insert an order, update a salary, or validate and record a request. It can still communicate results through parameters, but its call is an executable statement rather than part of an expression.
This procedure changes an employee's salary. CREATE OR REPLACE creates the procedure if it does not exist, or replaces its existing definition. The %TYPE attribute anchors the parameter to the type of the table column.
CREATE OR REPLACE PROCEDURE raise_salary (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER DEFAULT 10
) AS
BEGIN
UPDATE employees
SET salary = salary * (1 + p_percent / 100)
WHERE employee_id = p_employee_id;
END raise_salary;
/The slash is a client command used by tools such as SQL*Plus and SQL Developer. It sends the completed CREATE statement to Oracle. It is not part of the stored procedure itself.
Functions return values
Use a function when the caller needs one value. A function must declare a return type and execute a RETURN statement with a compatible value. A function can read or modify data, but functions intended for SQL expressions should follow additional restrictions.
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;
END get_full_name;
/A function can be called in an expression:
DECLARE
v_name VARCHAR2(100);
BEGIN
v_name := get_full_name(100);
DBMS_OUTPUT.PUT_LINE(v_name);
END;
/The caller treats the function result like a value. In contrast, a procedure call represents work:
BEGIN
raise_salary(100, 5);
END;
/Choosing between them
Ask what the caller needs. Choose a procedure when the primary purpose is an operation, especially one that changes state. Choose a function when the primary purpose is to produce a value that another expression can use.
A function is often a good fit for reusable calculations. A procedure is often clearer for workflows with several steps or side effects. Do not choose a function merely because it can return a value. A function that changes data can be surprising, especially when called from SQL.
Exercise
Which subprogram type should calculate an employee's annual bonus and return the calculated amount?
Show answer
Use a function because the caller needs one value.