Mysql Storing a variable with the result of an SELECT CASE

11,671

You need to use SELECT ... INTO

SELECT
...
INTO var_name [, var_name]]

instead of set to assign the result of a SELECT to a variable. So this should do it:

SELECT CASE 
WHEN @vgls=@lgls THEN "emp"
WHEN @vgls>@lgls THEN "loc"
WHEN @vgls<@lgls THEN "vis" END
INTO @st;
Share:
11,671
Enrique Benitez
Author by

Enrique Benitez

Updated on June 04, 2022

Comments

  • Enrique Benitez
    Enrique Benitez almost 2 years

    im learning how to use the SELECT CASE but i don't understand very well, here is the code:

    SET @vgls=1;
    SET @lgls=1;
    
    
    SET @st=SELECT CASE 
    WHEN @vgls=@lgls THEN "emp"
    WHEN @vgls>@lgls THEN "loc"
    WHEN @vgls<@lgls THEN "vis" END;
    
    SELECT @st;
    

    It gives me the following error:

    [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to you  MySQL server version for the right syntax to use near 'SELECT CASE 
    WHEN @vgls=@lgls THEN "emp"
    WHEN @vgls>@lgls THEN "loc"
    WHEN @vg' at line 1