 |
|
Oracle Tips by Burleson |
Default Values
When a variable is defined in a procedure or
function, a default value can optionally be assigned. When the
procedure is called, the passed variables are assigned in order; those
that are not assigned use the default value. If the default values of
leading variables should be used, then the variables must be passed by
name so that they are correctly assigned to the procedure’s variables.
SQL> create
or replace procedure example_defaults
2 (n_1 in number := 5,
3 n_2 in number := 6,
4 n_3 in number := 7)
5 as
6 begin
7 dbms_output.put_line(n_1||n_2||n_3);
8 end;
9 /
Procedure
created.
The code fragment above defines a procedure called
example_defaults that has three variables. In the code on line
7, we see that three variables are sent to the buffer for display.
The method the programmer uses (e.g. named variable, list of
variables) to call the procedure will determine which values are
passed and which will use the default values.
SQL> begin
2 example_defaults(7,8,9);
3 example_defaults(7,8);
4 example_defaults(7);
5 example_defaults();
6 end;
7 /
789
787
767
567
PL/SQL
procedure successfully completed.
Notice that the variables are assigned in the
order that they appear, and those variables that are not assigned will
use their default values. If they are not passed in order, they must
be passed by name.
SQL> begin
2 example_defaults(7,8,9);
3 example_defaults(n_2=>7,n_3=>8);
4 example_defaults(n_3=>3);
5 example_defaults(n_3=>5);
6 end;
7 /
789
578
563
565
All the values not passed by name to the procedure
will take their default value. If a variable is not assigned a
default value, then the call to that procedure must assign it a value
or the call will fail.
The above book excerpt is from:
Easy Oracle PL/SQL Programming
Get Started
Fast with Working PL/SQL Code Examples
ISBN 0-9759135-7-3
John Garmany
http://www.rampant-books.com/book_2005_1_easy_plsql.htm
|