Sub topic 7
Calling Subprograms from Blocks, Other Code, and Tools
This chapter assumes that you know basic SQL and anonymous PL/SQL blocks. You should also know how DECLARE, BEGIN, EXCEPTION, and END work.
A stored subprogram is called by its name. A procedure performs an action. A function returns one value and can appear in an expression.
Calling a procedure from a block
Call a procedure as a statement inside the executable part of a block:
BEGIN
raise_salary(
p_employee_id => 100,
p_percent => 5
);
END;
/The slash tells tools such as SQL*Plus and SQLcl to send the completed PL/SQL block to Oracle.
A procedure can call another procedure. The called procedure runs before control returns to the caller.
CREATE OR REPLACE PROCEDURE give_bonus (
p_employee_id IN employees.employee_id%TYPE
) AS
BEGIN
raise_salary(
p_employee_id => p_employee_id,
p_percent => 3
);
END give_bonus;
/The inner call uses named notation. Each argument names its matching parameter, so the arguments do not have to appear in declaration order.
Calling functions
A function call produces a value. Store that value in a variable or use it in an expression:
DECLARE
v_name VARCHAR2(100);
BEGIN
v_name := get_full_name(100);
IF v_name IS NULL THEN
DBMS_OUTPUT.PUT_LINE('No employee found');
ELSE
DBMS_OUTPUT.PUT_LINE('Employee: ' || v_name);
END IF;
END;
/You can also call a stored function from a SQL statement:
SELECT employee_id,
get_full_name(employee_id) AS full_name
FROM employees
WHERE department_id = 60;Oracle evaluates the function for rows selected by the query. The function must follow the restrictions for functions called from SQL. For example, it should not change database state while the query is running.
Choosing argument notation
Positional notation matches arguments by their order. Named notation matches them by parameter name. Default values let callers omit optional arguments.
| Call | Meaning |
|---|---|
| `raise_salary(101)` | Uses positional notation and the default percentage |
| `raise_salary(101, 5)` | Supplies both values positionally |
| `raise_salary(p_percent => 5, p_employee_id => 101)` | Uses named notation in a different order |
You can combine positional and named notation, but positional arguments must come first:
BEGIN
raise_salary(101, p_percent => 5);
END;
/Receiving output parameters
A procedure with OUT parameters returns values through variables supplied by the caller:
DECLARE
v_min NUMBER;
v_max NUMBER;
BEGIN
get_salary_range('IT_PROG', v_min, v_max);
DBMS_OUTPUT.PUT_LINE(
'Range: ' || v_min || ' - ' || v_max
);
END;
/An OUT argument must be writable, such as a variable. A literal cannot receive a returned value.
Calling from development tools
In SQL Developer, SQLcl, and SQL*Plus, run stored definitions and anonymous blocks as scripts. Enable server output when you want to see DBMS_OUTPUT:
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.PUT_LINE(get_full_name(100));
END;
/A client application usually invokes a procedure through a database driver. It binds input variables, registers OUT variables, executes the call, and reads the returned values.
Exercise
Which call uses named notation and omits the defaulted percentage parameter?
Show answer
raise_salary(p_employee_id => 101).