Variables in Stored Programs
1139
For information about the scope of local variables and how MySQL resolves ambiguous names, see
Section 13.6.4.2, “Local Variable Scope and Resolution”
.
13.6.4.1. Local Variable
DECLARE
Syntax
DECLARE
var_name
[,
var_name
] ...
type
[DEFAULT
value
]
This statement declares local variables within stored programs. To provide a default value for a
variable, include a
DEFAULT
clause. The value can be specified as an expression; it need not be a
constant. If the
DEFAULT
clause is missing, the initial value is
NULL
.
Local variables are treated like stored routine parameters with respect to data type and overflow
checking. See
Section 13.1.9, “
CREATE PROCEDURE
and
CREATE FUNCTION
Syntax”
.
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
Section 9.2, “Schema Object Names”
.
The scope of a local variable is the
BEGIN ... END
block within which it is declared. The variable can
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
BEGIN ... END
block within which it is declared. The variable can
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,
SELECT ... INTO local_var
cannot be used as a prepared statement. This restriction also applies to stored procedure and function
parameters. See
Section 13.5.1, “
PREPARE
Syntax”
.
A local variable should not have the same name as a table column. If an SQL statement, such as a
SELECT ... INTO
statement, contains a reference to a column and a declared local variable with
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
xname
in the
SELECT
statement as a reference to the
xname
variable rather than the
xname
column. Consequently, when the procedure
sp1()
is called, the
newname
variable returns the
value
'bob'
regardless of the value of the
table1.xname
column.
Similarly, the cursor definition in the following procedure contains a
SELECT
statement that refers
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);
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...