Oracle 5.0 Reference Manual page 782

Table of Contents

Advertisement

a permissible type. For example, a value having a temporal or spatial data type is converted to a binary
string.
If a user variable is assigned a nonbinary (character) string value, it has the same character set and
collation as the string. The coercibility of user variables is implicit as of MySQL 5.0.3. (This is the same
coercibility as for table column values.)
Bit values assigned to user variables are treated as binary strings. To assign a bit value as a number to
a user variable, use
CAST()
mysql>
SET @v1 = b'1000001';
mysql>
SET @v2 = CAST(b'1000001' AS UNSIGNED), @v3 = b'1000001'+0;
mysql>
SELECT @v1, @v2, @v3;
+------+------+------+
| @v1
| @v2
| @v3
+------+------+------+
| A
|
65 |
65 |
+------+------+------+
If the value of a user variable is selected in a result set, it is returned to the client as a string.
If you refer to a variable that has not been initialized, it has a value of
User variables may be used in most contexts where expressions are permitted. This does not currently
include contexts that explicitly require a literal value, such as in the
statement, or the
IGNORE N LINES
As a general rule, you should never assign a value to a user variable and read the value within
the same statement. You might get the results you expect, but this is not guaranteed. The order
of evaluation for expressions involving user variables is undefined and may change based on the
elements contained within a given statement; in addition, this order is not guaranteed to be the same
between releases of the MySQL Server. In
MySQL will evaluate
@a
(for example, by adding a
execution plan with a different order of evaluation.
Another issue with assigning a value to a variable and reading the value within the same statement is
that the default result type of a variable is based on its type at the start of the statement. The following
example illustrates this:
mysql>
SET @a='test';
mysql>
SELECT @a,(@a:=20) FROM tbl_name;
For this
statement, MySQL reports to the client that column one is a string and converts all
SELECT
accesses of
to strings, even though @a is set to a number for the second row. After the
@a
statement executes,
@a
To avoid problems with this behavior, either do not assign a value to and read the value of the same
variable within a single statement, or else set the variable to 0, 0.0, or
use it.
In a
statement, each select expression is evaluated only when sent to the client. This means
SELECT
that in a HAVING,
GROUP
the select expression list does not work as expected:
mysql>
SELECT (@aa:=id) AS a, (@aa+3) AS b FROM tbl_name HAVING b=5;
The reference to
in the
b
@aa. This does not work as expected:
from the current row.
User variables are intended to provide data values. They cannot be used directly in an SQL statement
as an identifier or as part of an identifier, such as in contexts where a table or database name is
User-Defined Variables
[948]
or +0:
|
clause of a
SELECT @a, @a:=@a+1,
first and then do an assignment second. However, changing the statement
BY, HAVING, or
GROUP
is regarded as a number for the next statement.
BY, or
clause, referring to a variable that is assigned a value in
ORDER BY
clause refers to an alias for an expression in the select list that uses
HAVING
contains the value of
@aa
762
NULL
LIMIT
statement.
LOAD DATA
clause) may cause MySQL to select an
ORDER BY
''
from the previous selected row, not
id
and a type of string.
clause of a
SELECT
..., you might think that
SELECT
to define its type before you

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents