 |
|
Oracle Tips by Burleson |
Passing
Variables
If a procedure or function is going to integrate
into an application, it must perform some action that is useful. This
means that the application must be able to pass the module information
and receive information back. This is accomplished by passing
variables to the module that it can use in performing the task. In
the case of a procedure, variables can be read, used and changed. A
function can read and use a variable, but can not change it (a
function returns a value, more on this in a moment). How a procedure
uses a variable will be covered shortly, here we just want to discuss
how they are passed to the procedure or function.
Variables passed to procedures and functions
are defined in the block
definition.
create
procedure full_user_name
(first_name in varchar2,
last_name in varchar2,
full_name out varchar2)
as
This example is the heading of a procedure called
full_user_name that passes three variables: first_name;
last_name; and full_name. These three variable names
are used inside the procedure and are in scope only inside the
procedure. In the example the variable values are assigned when
passed.
declare
v_fullName varchar2(80);
v_first varchar2(20) := 'Bill';
v_last varchar2(40) := 'Smithadoodle';
begin
full_user_name ('Sam','Smith', v_fullName);
full_user_name (v_first,v_last,v_fullName);
In the code fragment above, the procedure
full_name is called twice. The first call passes literal values
to the procedure, while the second invocation passed predefined
variables. The order that the variables are passed is important as
PL/SQL will assign them to the procedure’s variables in the order that
they were passed to the procedure. If the variables are passed out of
order, they must be passed by name as in the code fragment below.
declare
v_fullName vatchar2(80);
v_first varchar2(20) := 'Bill';
v_last varchar2(40) := 'Smithadoodle';
begin
full_user_name (v_last => last_name,
v_fullName => full_name,
v_first => first_name);
Here
the parameters are passed out of order and must name the procedure
variables they are to be assigned. Passing by name requires that the
developer know the name or the variables in the block definition. You
can use the DESCRIBE command
in SQL*Plus
to see the variables and
their names. Normally you will pass the variables in order but there
are cases when you may want to pass by name. The most common reason
to pass a variable by name is to use default values.
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
|