Using Default Values in Parameters v12
You can set a default value of a formal parameter by including the DEFAULT
clause or using the assignment operator (:=)
in the CREATE PROCEDURE
or CREATE FUNCTION
statement.
The general form of a formal parameter declaration is:
name
is an identifier assigned to the parameter.
IN|OUT|IN OUT
specifies the parameter mode.
data_type
is the data type assigned to the variable.
expr
is the default value assigned to the parameter. If you do not include a DEFAULT
clause, the caller must provide a value for the parameter.
The default value is evaluated every time the function or procedure is invoked. For example, assigning SYSDATE
to a parameter of type DATE
causes the parameter to have the time of the current invocation, not the time when the procedure or function was created.
The following simple procedure demonstrates using the assignment operator to set a default value of SYSDATE
into the parameter, hiredate:
If the parameter declaration includes a default value, you can omit the parameter from the actual parameter list when you call the procedure. Calls to the sample procedure (hire_emp)
must include two arguments: the employee number (p_empno)
and employee name (p_empno)
. The third parameter (p_hiredate)
defaults to the value of SYSDATE:
If you do include a value for the actual parameter when you call the procedure, that value takes precedence over the default value:
Adds a new employee with a hiredate of February 15, 2010
, regardless of the current value of SYSDATE
.
You can write the same procedure by substituting the DEFAULT
keyword for the assignment operator: