SELECT INTO Variable in MySQL DECLARE causes syntax error?

前端 未结 11 2138
無奈伤痛
無奈伤痛 2020-11-30 21:48

I´d like to SELECT a single value into a variable. I´d tried to following:

DECLARE myvar INT(4);

-- immediately returns some syntax error.<

11条回答
  •  孤独总比滥情好
    2020-11-30 22:05

    You don't need to DECLARE a variable in MySQL. A variable's type is determined automatically when it is first assigned a value. Its type can be one of: integer, decimal, floating-point, binary or nonbinary string, or NULL value. See the User-Defined Variables documentation for more information:

    http://dev.mysql.com/doc/refman/5.0/en/user-variables.html

    You can use SELECT ... INTO to assign columns to a variable:

    http://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html

    Example:

    mysql> SELECT 1 INTO @var;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> SELECT @var;
    +------+
    | @var |
    +------+
    | 1    |
    +------+
    1 row in set (0.00 sec)
    

提交回复
热议问题