MySQL Case Select not behaving as expected

后端 未结 2 1733
[愿得一人]
[愿得一人] 2021-01-26 17:01

While creating a more complex stored procedure in MySQL, I encountered a weird problem with my CASE statement. I have simplified my procedure to show the issue. I am selecting t

2条回答
  •  有刺的猬
    2021-01-26 17:52

    Shouldn't it be more like

    CREATE PROCEDURE `AddPlants` (IN plantNum int)
    

    BEGIN DECLARE count int; DECLARE modTemp int;

    SET count = 1;
    WHILE count <= plantNum DO
        SELECT CONCAT('count = ', count);
        SET modTemp = count % 3;
        SELECT CONCAT('modTemp = ', modTemp);
    
        CASE modTemp
            WHEN 1 THEN
                SELECT 1;
            WHEN 2 THEN
                SELECT 2;
            WHEN 0 THEN
                SELECT 3;
            ELSE
                SELECT CONCAT('Error: modTemp = ', modTemp);
        END CASE;
    
        SET count = count + 1;
    END WHILE;
    

    END//

提交回复
热议问题