Http://www.cnblogs.com/qixuejia/archive/2010/12/21/1913203.html
The variables in SQL Server are first declared and then assigned:
Local variables with an @ identifier, global variables with two @ (common global variables are generally defined);
Declare local variable syntax: DECLARE @ variable name data type; For example: declare @num int;
Assignment: There are two methods (@num is the variable name, value is the values)
Set @num =value; or select @num =value;
If you want to get a field value in a query statement, you can assign a value to a variable with SELECT, as follows:
Select @num = field name from table name where ...
The variables in MySQL do not have to be declared beforehand, use "@ variable name" when using it.
First usage: Set @num = 1; or set @num: = 1; To use variables to save the data, use the @num variable directly
Second usage: select @num: = 1; or select @num: = field name from table name where ...
Note the above two assignment symbols, which can be "=" or ": =" when using set, but must use ": = Assignment" When using Select
=================
Http://dev.mysql.com/doc/refman/5.7/en/user-variables.html
user-defined Variables
You can store a value in a user-defined variable in one statement and then refer to it later in another statement. This enables-pass values from one statement to another.
User variables is written as @ var_name
, where the variable name var_name
consists of Alphanumeric Characters, "", < span class= "quote" > "_
", And "$
". A user variable name can contain other characters if you quote it as a string or identifier (for Example, @ ' My-var '
, @ "My-var"
, Or @ ' My-var '
).
user-defined variables is session-specific. A user variable defined by one client cannot is seen or used by other clients. (exception:a user with access to the Performance Schema user_variables_by_thread
table can see all user variables for all sessions.) All variables for a given client session is automatically freed when that client exits.
User variable names is not case sensitive. Names has a maximum length of characters as of MySQL 5.7.5. (Length is not constrained before.)
One-to-set a user-defined variable is by issuing a SET
statement:
SET @ var_name
expr
[, @ var_name
] ... expr
SET
for, either =
or :=
can is used as the assignment operator.
You can also assign a value to a user variable in statements other than SET
. In this case, the assignment operator must are and not because the latter are treated as the comparison :=
=
operator In =
non- SET
statements:
mysql> SET @t1 =1, @t2 =2, @t3: =4;
mysql> select @t1, @t2, @t3, @t4: = @[email protected][email protected];
+------+------+------+--------------------+| @t1 | @t2 | @t3 | @t4: = @[email protected][email protected] |+------+------+------+--------------------+| 1 | 2 | 4 | 7 |+------+------+------+--------------------+
User variables can assigned a value from a limited set of data Types:integer, decimal, floating-point, binary or Nonbi Nary string, or NULL
value. Assignment of decimal and real values does not preserve the precision or scale of the value. A value of a type other than one of the permissible types are converted to a permissible type. For example, a value has a temporal or spatial data type is converted to a binary string. A value has the JSON
data type is converted to a string with a character set of and utf8mb4
a collation of utf8mb4_bin
.
If a user variable is assigned a nonbinary (character) string value, it has the same character set and collation as the St Ring. The coercibility of user variables is implicit. (This was the same coercibility as for table column values.)
Bit values assigned to user variables is treated as binary strings. To assign a bit value as a number to a user variable, use CAST()
or +0
:
SET @v1 = b‘1000001‘;
SET @v2 = CAST(b‘1000001‘ AS UNSIGNED), @v3 = b‘1000001‘+0;
SELECT @v1, @v2, @v3;
+------+------+------+| @v1 | @v2 | @v3 |+------+------+------+| A | | |+------+------+------+
If the value of a user variable is selected in a result set, it's returned to the client as a string.
If you refer to a variable which has not been initialized, it has a value of and NULL
a type of string.
User variables may used in the most contexts where expressions is permitted. This does isn't currently include contexts that explicitly require a literal value, such as in the LIMIT
clause of a SELECT
s Tatement, or the IGNORE N
LINES
clause of a LOAD DATA
statement.
As a general rule, other than SET
in statements, you should never assign a value to a user variable and read the value W Ithin the same statement. For example, to increment a variable, this is okay:
SET @a = @a + 1;
For other statements, such as SELECT
, you might get the results to expect, but this is not guaranteed. In the following statement, you might think that MySQL would evaluate first and then does an @a
assignment second:
SELECT @a, @a:[email protected]+1, ...;
However, the order of evaluation for expressions involving user variables is undefined.
Another issue with assigning a value to a variable and reading the value within the same non-statement are that the SET
D Efault result type of a variable is based on its type at the start of the statement. The following example illustrates this:
SET @a=‘test‘;
tbl_name
;
For this SELECT
statement, MySQL reports to the client, the-column one is a string and converts all accesses of to @a
St Rings, even though @a is set to a number for the second row. After SELECT
the statement executes, are regarded as a number for the @a
Next statement.
To avoid problems with this behavior, either does not assign a value to and read the value of the same variable within a sin GLE statement, or else set the variable to 0
, 0.0
and or to ‘‘
define their type before you use it.
In a SELECT
statement, each select expression was evaluated only if sent to the client. This means a HAVING
, GROUP BY
, or ORDER BY
clause, referring to a variable, which is assigned a value in the Select Express Ion list does not work as expected:
tbl_name
HAVING b=5;
The reference to of the clause refers to a alias for a expression in the b
HAVING
select list that uses @aa
. This does is expected: @aa
contains id
the value of the from the previous selected row, not from the current row .
User variables is intended to provide data values. They cannot is 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 expected, or as a reserved word such asselect
. This is true even if the variable are quoted, as shown in the following example:
mysql> SELECT C1 from T;
+----+| C1 |+----+| 0 |+----+| 1 |+----+2 rows in Set (0.00 sec) mysql> set @col = "C1";
Query OK, 0 rows Affected (0.00 sec) mysql> SELECT @col from T;
+------+| @col |+------+| C1 |+------+1 row in Set (0.00 sec) mysql> SELECT ' @col ' from T;
error 1054 (42S22): Unknown column ' @col ' in ' Field List ' mysql> SET @col = "' C1 '"; Query OK, 0 rows Affected (0.00 sec) mysql> SELECT @col from T;
+------+| @col |+------+| ' C1 ' |+------+1 row in Set (0.00 sec)
An exception-to-principle that user variables cannot being used to provide identifiers, was when you were constructing a s Tring for use as a prepared statement to execute later. In this case, user variables can is used to provide any part of the statement. The following example illustrates how this can is done:
SET @c = "c1";
SET @s = CONCAT("SELECT ", @c, " FROM t");
PREPARE stmt FROM @s;
EXECUTE stmt;
+----+| C1 |+----+| 0 |+----+| DEALLOCATE PREPARE stmt;
Query OK, 0 rows Affected (0.00 sec)
Variable assignment in MySQL