case statement in stored procedure

2019-08-12 02:12发布

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条回答
何必那么认真
2楼-- · 2019-08-12 02:42

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!

查看更多
登录 后发表回答