Sub topic 6
Positional Notation, Named Notation, and Default Values
Prerequisites and the basic problem
This chapter assumes that you know basic SQL and PL/SQL blocks, including variables, BEGIN, END, and procedure calls. You should also understand Oracle data types and stored subprograms.
When a procedure or function has several parameters, a call must associate each actual value with the correct formal parameter. PL/SQL provides two main ways to do this:
- Positional notation matches values by their order.
- Named notation matches values by formal parameter name.
A formal parameter is the parameter declared by the subprogram. An actual parameter is the value or variable supplied by the caller.
Positional notation
With positional notation, the first value goes to the first parameter, the second value goes to the second parameter, and so on.
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;
/This call supplies both values in declaration order:
BEGIN
raise_salary(100, 5);
END;
/Positional notation is short, but it depends on remembering the parameter order. A swapped value can produce a compilation error or, worse, a valid but incorrect result.
Named notation
Named notation uses the formal parameter name followed by => and the actual value. The arguments can appear in any order.
BEGIN
raise_salary(
p_percent => 5,
p_employee_id => 100
);
END;
/Named notation makes the meaning of each value visible. It is often safer for calls with several parameters, especially when multiple parameters have compatible data types.
You can begin with positional notation and continue with named notation, but you cannot return to positional notation afterward:
BEGIN
raise_salary(100, p_percent => 5);
END;
/The reverse form is invalid because PL/SQL cannot identify a later positional value after named notation has started.
Default values
A default value is used when the caller omits an optional argument. In the example, p_percent defaults to 10.
BEGIN
raise_salary(101);
END;
/This is equivalent to:
BEGIN
raise_salary(101, 10);
END;
/Named notation is useful when you want to omit a parameter with a default while supplying another later parameter.
flowchart TD
A[Procedure declaration] --> B{Argument supplied?}
B -->|Yes| C[Use caller's value]
B -->|No| D[Use default value]Choosing a notation
| Situation | Suitable notation |
|---|---|
| Short call with stable parameter order | Positional |
| Many parameters or repeated data types | Named |
| Optional parameters with defaults | Named or positional |
| Public API calls that should be self-documenting | Named |
Common errors
- Supplying values in the wrong positional order.
- Misspelling a formal parameter name in named notation.
- Using a positional argument after a named argument.
- Assuming a default applies when
NULLis explicitly passed. PassingNULLsupplies a value; it does not omit the argument. - Omitting a parameter that has no default value.
Exercise
What value does p_percent receive in raise_salary(p_employee_id => 100)?
Show answer
It receives the declared default value, 10.