SELECT INTO Variable in MySQL DECLARE causes syntax error?

前端 未结 11 2127
無奈伤痛
無奈伤痛 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:28

    These answers don't cover very well MULTIPLE variables.

    Doing the inline assignment in a stored procedure causes those results to ALSO be sent back in the resultset. That can be confusing. To using the SELECT...INTO syntax with multiple variables you do:

    SELECT a, b INTO @a, @b FROM mytable LIMIT 1;
    

    The SELECT must return only 1 row, hence LIMIT 1, although that isn't always necessary.

提交回复
热议问题