Sub topic 6
Positional Notation, Named Notation, and Default Values
This chapter assumes you can write a basic anonymous block, you know the IN, OUT, and IN OUT parameter modes from the previous chapter, and you can create a procedure in your own schema. It covers how a caller maps values to formal parameters, how default values let a caller leave parameters out, and which mistakes cause compile-time and run-time errors.
Actual and formal parameters
A parameter in the subprogram declaration is a formal parameter. A value or variable in the call is an actual parameter. Oracle must pair every actual parameter with a formal parameter. There are three ways to express that pairing:
- Positional notation matches by order.
- Named notation matches by name, using the
=>operator. - Mixed notation starts with positional actuals and finishes with named ones.
The examples use this procedure from the earlier chapters:
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;
/Positional notation
In positional notation the first actual goes to the first formal, the second to the second, and so on. It is short, and it is the form most people use for subprograms with one or two parameters.
BEGIN
raise_salary(100, 5);
END;
/Here 100 goes to p_employee_id and 5 goes to p_percent. If you swap the two values, the code still compiles because both parameters are numeric. It then raises the salary of employee 5 by 100 percent, or fails with the "Employee not found" error. Positional calls are fragile when neighbouring parameters share a type.
Named notation
In named notation you write the formal parameter name, then =>, then the actual value. The order of the pairs does not matter.
BEGIN
raise_salary(p_percent => 5, p_employee_id => 100);
END;
/Named notation has three advantages:
- The call documents itself, which matters for calls like
process(TRUE, FALSE, NULL, 3). - You can skip any parameter that has a default, not only the trailing ones.
- The call survives a later reordering of the formal parameters.
It also has a cost. The formal parameter names become part of the subprogram's public interface. If you rename p_percent to p_pct, every named call to it fails to compile. Treat parameter names as part of the contract, especially inside packages that other teams call.
Mixed notation
You can begin a call with positional actuals and then switch to named ones. After the first named actual, every remaining actual must be named.
BEGIN
raise_salary(100, p_percent => 7);
END;
/The reverse is not allowed. raise_salary(p_employee_id => 100, 7) fails with PLS-00312 (a positional parameter association may not follow a named association). Mixed notation is useful when the first parameter is obvious and the later ones need labels.
The rules are summarized below.
| Style | Syntax | Order matters | Can skip a middle default |
|---|---|---|---|
| Positional | `f(a, b, c)` | Yes | No |
| Named | `f(p3 => c, p1 => a)` | No | Yes |
| Mixed | `f(a, p3 => c)` | Only for the positional part | Yes, among the named part |
Default values
A formal parameter can have a default value, written with either DEFAULT or :=. Both are equivalent.
CREATE OR REPLACE PROCEDURE log_message (
p_text IN VARCHAR2,
p_severity IN VARCHAR2 := 'INFO',
p_source IN VARCHAR2 DEFAULT 'APP'
) AS
BEGIN
DBMS_OUTPUT.PUT_LINE(p_severity || ' [' || p_source || '] ' || p_text);
END log_message;
/When the caller omits a parameter that has a default, Oracle evaluates the default and uses it. Here are three valid calls:
SET SERVEROUTPUT ON
BEGIN
log_message('Job started');
log_message('Disk almost full', 'WARN');
log_message('Payment failed', p_source => 'BILLING');
END;
/The output is:
INFO [APP] Job started
WARN [APP] Disk almost full
INFO [BILLING] Payment failedThe third call shows the main reason to use named notation with defaults. With positional notation you cannot skip p_severity and still reach p_source.
Where defaults are allowed
Defaults apply only to IN parameters. An OUT or IN OUT parameter cannot have a default, and the compiler rejects the declaration with PLS-00230. The reason is that an OUT parameter is a place where the subprogram writes a value, so there is no incoming value to default.
Because defaults are evaluated at call time, a default can be an expression and not only a literal, for example p_date IN DATE DEFAULT SYSDATE. A default can also refer to constants in a package. It cannot refer to other parameters of the same subprogram.
Required parameters before optional ones
Positional notation can only omit parameters from the end of the list. A common design is to place required parameters first and optional ones last. In raise_salary, the employee ID has no default and the percentage has one. A call that omits a parameter with no default fails to compile with PLS-00306 (wrong number or types of arguments in call to the subprogram).
Passing NULL is not the same as omitting
Omitting a parameter means "use the default". Passing NULL means "use the value NULL". These are different, and the difference is a frequent source of bugs.
BEGIN
raise_salary(100); -- percent = 10
raise_salary(100, NULL); -- percent = NULL
END;
/The second call does not raise an error. The expression salary * (1 + NULL / 100) evaluates to NULL, so the salary of employee 100 is set to NULL. If a column has a NOT NULL constraint, the update fails instead, with ORA-01407. Neither outcome is what the caller intended. If NULL should mean "use the default", handle it explicitly at the top of the body:
CREATE OR REPLACE PROCEDURE raise_salary_safe (
p_employee_id IN employees.employee_id%TYPE,
p_percent IN NUMBER DEFAULT 10
) AS
v_percent NUMBER := NVL(p_percent, 10);
BEGIN
UPDATE employees
SET salary = salary * (1 + v_percent / 100)
WHERE employee_id = p_employee_id;
END raise_salary_safe;
/This repeats the literal 10. For anything beyond a demo, define the default once as a package constant and use it in both places.
Notation in functions and in SQL
Functions follow the same rules. Within PL/SQL you can use any notation:
DECLARE
v_name VARCHAR2(100);
BEGIN
v_name := get_full_name(p_employee_id => 100);
DBMS_OUTPUT.PUT_LINE(v_name);
END;
/In a SQL statement, named and mixed notation are supported for stored functions in Oracle 11g and later. Earlier releases accepted only positional notation in SQL, so older code often shows it. If you maintain code that must run on an old version, check which notations your release supports.
SELECT employee_id,
get_full_name(p_employee_id => employee_id) AS full_name
FROM employees
WHERE department_id = 60;Named notation in SQL has one more use. A function with several defaulted parameters can be called with only the ones you care about, without a long list of placeholders.
Overloading and notation
When a package contains overloaded subprograms, Oracle resolves the call by comparing the actual parameters to each candidate. Named notation can make a call ambiguous, because two overloads may share the same parameter names. If more than one candidate fits, the compiler reports PLS-00307 (too many declarations match the call). Defaults add to this risk: two overloads that differ only in an optional trailing parameter can both match a shorter call. Keep overloads clearly different in the number or types of required parameters.
How the compiler checks a call
The compiler resolves every call when it compiles the calling unit, not when the call runs. It checks that:
- Each named actual matches a formal parameter name.
- No formal parameter receives two values, for example one positional and one named for the same formal.
- Every formal without a default receives a value.
- Each actual has a type compatible with its formal, and OUT and IN OUT actuals are variables and not literals.
Failures in these checks appear when you compile the caller, so you can read them from USER_ERRORS as shown in the compilation chapter. A call that passes the same parameter twice, such as raise_salary(100, p_employee_id => 101), fails with PLS-00306.
Choosing a style
Use these guidelines when deciding how to write a call:
- Use positional notation for one or two parameters with distinct types and obvious meaning.
- Use named notation when a call has three or more parameters, several literals of the same type, or any boolean flag.
- Use named notation whenever you skip a defaulted parameter.
- Keep required parameters first and optional parameters last in the declaration.
- Give default values to parameters only when a sensible default exists. A default that hides a required decision makes bugs harder to find.
Exercise
Given the procedure logmessage(ptext IN VARCHAR2, pseverity IN VARCHAR2 := 'INFO', psource IN VARCHAR2 DEFAULT 'APP'), write a call that sets the source to 'AUDIT' and the severity to its default. Then explain why log_message('Done', 'AUDIT') does not do the same thing.
Show answer
Use named notation to skip pseverity: logmessage('Backup finished', psource => 'AUDIT'). The call logmessage('Done', 'AUDIT') is positional, so 'AUDIT' goes to the second formal parameter, pseverity, and psource keeps its default 'APP'.
Exercise
A procedure has the signature calc(pa IN NUMBER, pb IN NUMBER DEFAULT 1, p_c OUT NUMBER). Why does the declaration fail, and how do you fix it?
Show answer
An OUT parameter cannot have a default, but the real error here is that pb has a default while pc does not. The declaration is legal, because a defaulted IN parameter may come before a non-defaulted one. The problem appears at the call: a positional call must supply pc, so pb cannot be skipped positionally. Callers must write calc(5, pc => vresult) with named notation for pc, or you should reorder the parameters so that pc comes before the defaulted p_b.