Sub topic 2
Positional notation, named notation, and default values
This chapter assumes you know basic SQL (SELECT, UPDATE), PL/SQL anonymous blocks (DECLARE, BEGIN, EXCEPTION, END), and have the CREATE PROCEDURE privilege in a schema. The examples use the employees table from the HR sample schema.
Positional and named notation
A subprogram is a named PL/SQL unit. A procedure performs an action, and a function returns a value. Callers pass arguments, and PL/SQL matches each argument to a parameter in one of two ways:
- Positional notation matches arguments by their order.
- Named notation matches arguments by parameter name, using the
=>operator. Order does not matter.
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;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Employee not found: ' || p_employee_id);
END IF;
END raise_salary;
/The DEFAULT 10 clause lets callers omit p_percent. In a mixed call, positional arguments must come first, followed by named ones.
BEGIN
raise_salary(p_percent => 5, p_employee_id => 100);
raise_salary(101);
END;
/Calling subprograms from blocks, other code, and tools
Any client that sends PL/SQL can call a subprogram. That includes an anonymous block in SQL*Plus or SQL Developer, another stored subprogram, or an application that runs a block through its database driver. A function is called inside an expression, for example v_name := get_full_name(100);.
Handling and raising errors
RAISE_APPLICATION_ERROR raises a user-defined error. The error number must fall between -20000 and -20999. An exception that no handler catches passes to the caller.
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;
/Calling stored functions from SQL
You can call a function in a query, and Oracle runs it once per row:
SELECT employee_id, get_full_name(employee_id) AS full_name
FROM employees
WHERE department_id = 60;A function used in SQL must follow these rules:
- All parameters must be
IN.OUTandIN OUTparameters are not allowed. - The return type must be a SQL type such as NUMBER or VARCHAR2. PL/SQL-only types such as BOOLEAN are not allowed.
- The function must not run DML (INSERT, UPDATE, DELETE), COMMIT, or ROLLBACK. Oracle raises an error if it does.
Exercise
Exercise
The call raise_salary(101) updates no rows. What does the caller see, and how would you change the call to pass a 3 percent raise to employee 101 using named notation?
Show answer
Because SQL%ROWCOUNT is 0, the procedure raises ORA-20001 with the message "Employee not found: 101", which propagates to the caller. Use raisesalary(pemployeeid => 101, ppercent => 3); to pass a 3 percent raise.