User-Defined Variables 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  NULL  and a type of string. Beginning with MySQL 8.0.22, a reference to a user variable in a prepared statement has its type determined when the statement is first prepared, and retains this type each time the statement is executed thereafter. Similarly, the type of a user variable employed in a statement within a stored procedure is determined the first time the stored procedure is invoked, and retains this type with each subsequent invocation. 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  LIMIT  clause of a  SELECT  statement, or the  IGNORE  N  LINES  clause of a  LOAD DATA  statement. Previous releases of MySQL made it possible to assign a value to a user variable in statements other than SET . This functionality is supported in MySQL 8.0 for backward compatibility but is subject to removal in a future release of MySQL. When making an assignment in this way, you must use  :=  as the assignment operator;  =  is treated as the comparison operator in statements other than  SET . The order of evaluation for expressions involving user variables is undefined. For example, there is no guarantee that  SELECT @a, @a:=@a+1  evaluates  @a  first and then performs the assignment. In addition, the default result type of a variable is based on its type at the beginning of the statement. This may have unintended effects if a variable holds a value of one type at the beginning of a statement in which it is also assigned a new value of a different type. 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  ’’  to define its type before you use it. HAVING ,  GROUP BY , and  ORDER BY , when referring to a variable that is assigned a value in the select expression list do not work as expected because the expression is evaluated on the client and thus can use stale column values from a previous 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 expected, or as a reserved word such as  SELECT . This is true even if the variable is 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 | +------+ 1913