case statement in stored procedure

北城余情 提交于 2019-12-11 05:55:42

问题


I have this stored procedure, its working

CREATE PROCEDURE myrange()
BEGIN
SET @start = 0;
PREPARE stmt FROM ' SELECT firstname FROM peoples ORDER BY id LIMIT ?,1 INTO @name';
    WHILE @start < 5 DO
        EXECUTE stmt USING @start;
        CASE 
            WHEN @name = 'Ana'
                THEN INSERT INTO mytable (log) VALUES('yes');
            ELSE 
                INSERT INTO mytable (log) VALUES('no');
        END CASE;
        SET @start = @start + 1;
    END WHILE;
END;

but, if I delete from this procedure, this piece of code:

ELSE 
    INSERT INTO mytable (log) VALUES('no');

mysqli_error() returns this error: "Case not found for CASE statement".

Someone know, why?


回答1:


From the MySQL CASE syntax

If no when_value or search_condition matches the value tested and the CASE statement contains no ELSE clause, a Case not found for CASE statement error results.

You must have all options covered.

The MySQL manual is a great documentation. Be sure to visit it!



来源:https://stackoverflow.com/questions/11483167/case-statement-in-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!