Oracle 5.0 Reference Manual page 1159

Table of Contents

Advertisement

For information about the scope of local variables and how MySQL resolves ambiguous names, see
Section 13.6.4.2, "Local Variable Scope and
13.6.4.1. Local Variable
DECLARE
var_name
This statement declares local variables within stored programs. To provide a default value for a
variable, include a
constant. If the
Local variables are treated like stored routine parameters with respect to data type and overflow
checking. See
Variable declarations must appear before cursor or handler declarations.
Local variable names are not case sensitive. Permissible characters and quoting rules are the same as
for other identifiers, as described in
The scope of a local variable is the
be referred to in blocks nested within the declaring block, except those blocks that declare a variable
with the same name.
13.6.4.2. Local Variable Scope and Resolution
The scope of a local variable is the
be referred to in blocks nested within the declaring block, except those blocks that declare a variable
with the same name.
Because local variables are in scope only during stored program execution, references to them are not
permitted in prepared statements created within a stored program. Prepared statement scope is the
current session, not the stored program, so the statement could be executed after the program ends, at
which point the variables would no longer be in scope. For example,
cannot be used as a prepared statement. This restriction also applies to stored procedure and function
parameters. See
A local variable should not have the same name as a table column. If an SQL statement, such as a
SELECT ... INTO
the same name, MySQL currently interprets the reference as the name of a variable. Consider the
following procedure definition:
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE newname VARCHAR(5);
DECLARE xid INT;
SELECT xname, id INTO newname, xid
FROM table1 WHERE xname = xname;
SELECT newname;
END;
MySQL interprets
column. Consequently, when the procedure sp1()is called, the
xname
value
'bob'
Similarly, the cursor definition in the following procedure contains a
to xname. MySQL interprets this as a reference to the variable of that name rather than a column
reference.
CREATE PROCEDURE sp2 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE newname VARCHAR(5);
Variables in Stored Programs
Syntax
DECLARE
[, var_name] ...
clause. The value can be specified as an expression; it need not be a
DEFAULT
clause is missing, the initial value is NULL.
DEFAULT
Section 13.1.9,
"CREATE PROCEDURE
Section 9.2, "Schema Object
BEGIN ... END
BEGIN ... END
Section 13.5.1,
"PREPARE
statement, contains a reference to a column and a declared local variable with
in the
xname
SELECT
regardless of the value of the
Resolution".
type
[DEFAULT value]
and
CREATE FUNCTION
block within which it is declared. The variable can
block within which it is declared. The variable can
Syntax".
statement as a reference to the
table1.xname
1139
Syntax".
Names".
SELECT ... INTO local_var
variable rather than the
xname
variable returns the
newname
column.
statement that refers
SELECT

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents