Ted
Ted

Reputation: 4166

MySQL: use "IF NOT IN" in procedure

I'm trying to check class using MySQL procedure, but the following procedure always returns 0:

DELIMITER //
CREATE PROCEDURE `validate_class`(IN `class` INT)
BEGIN
    if(class NOT IN  ('A','B','E') ) then    
         select 1;
    else
         select 0;
    end if;
END //
DELIMITER ;

call test:

call validate_class('G'); //return 0
call validate_class('A'); //return 0

It should return 1 when class isn't (A and B and E), any help?

Upvotes: 3

Views: 528

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175954

You have implicit conversions CHAR -> INT -> CHAR.

Change parameter datatype:

CREATE PROCEDURE `validate_class`(IN `class` CHAR(1))
BEGIN
    if(class NOT IN  ('A','B','E') ) then    
         select 1;
    else
         select 0;
    end if;
END 

SqlFiddleDemo

Upvotes: 3

Related Questions