Question

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?

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top