MySQL: use "IF NOT IN" in procedure - mysql

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?

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

Related

Create a stored procedure with if beetween queries MySQL

I am looking to convert this query from SQL Server to Mysql:
create procedure Valor
#valor int
as
begin
if #valor = 1
begin
select 1
end
else
begin
select 2
end
end
As its a function:
CREATE FUNCTION Valor (valor int)
returns int DETERMINISTIC
return if(valor=1,1,2);
This is what served me.
CREATE PROCEDURE mysp ()
BEGIN
IF ##server_id=2 THEN DROP DATABASE accounting; END IF;
END;

using if else in mysql user defined function

This is my query that basically takes two numbers that adds them and multiplies the sum by 10
DELIMITER $$
CREATE FUNCTION tot(a int(4),b INT(4)) RETURNS INT(4)
BEGIN
RETURN ROUND((a+b)*10/9);
END $$
DELIMITER ;
everything is working fine , but I was wondering if there was a way I could add an IF ELSE
that checks if any of of the values entered is null and if so the null value is assigned a value of zero
I've tried this but I'm getting an error
DELIMITER $$
CREATE FUNCTION tot(a int(4),b INT(4)) RETURNS INT(4)
BEGIN
IF (a = "") then
a=0;
ELSE IF (b = "")
b=0;
ELSE
END IF;
RETURN ROUND((a+b)*10/9);
END $$
DELIMITER ;
I was wondering if there was a way I could add an IF ELSE that checks if any of of the values entered is null and if so the null value is assigned a value of zero
You can just use coalesce() to assign a default to null parameters:
DELIMITER $$
CREATE FUNCTION tot(a int(4),b INT(4)) RETURNS INT(4)
BEGIN
RETURN ROUND((COALESCE(a, 0) + COALESCE(b, 0)) * 10 / 9);
END $$
DELIMITER ;

MySQL IF statement procedure creation error

why won't this run? Complains of error when I try to create the procedure.
CREATE PROCEDURE `xxx_testIF`
(
IN _memberId int
)
BEGIN
IF _memberId = 0
select * from incident_report
ELSE
select * from incident_report where filed_by = _memberId
END IF;
END
Always use Delimiters while creating procedures and also you have missing THEN after the IF condition.
Try this:
DELIMITER $$
CREATE PROCEDURE `xxx_testIF`
(
IN _memberId int
)
BEGIN
IF _memberId = 0 THEN
select * from incident_report;
ELSE
select * from incident_report where filed_by = _memberId;
END IF;
END$$
DELIMITER ;

MySQL procedure that return true / false

I have procedure in MySQL called 'luhn_'.
I would like to use it like function in my other procedure - in IF condition. So it should return me some output parameter (maybe true / false?)
What is the easiest way to implement something like that?
DELIMITER $$
DROP procedure if exists `luhn_`$$
CREATE procedure `luhn_` (IN input_string VARCHAR(256), IN input_lenght INTEGER)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght)
THEN SELECT count(*) from courrier_envoye; -- true
ELSE
SELECT * from courrier_envoye; -- false
END IF;
END$$
-- call luhn_('123456789',10);
if condition
...
if(luhn_('123456789',10) = true) THEN ...
ELSE ...
...
Creating a function would do it for you
DELIMITER $$
CREATE FUNCTION `luhn_`(input_string VARCHAR(256), input_lenght INTEGER) RETURNS tinyint(1)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght) THEN
return true;
ELSE
return false;
END IF;
END
Usage in a stored procedure
if(select luhn_(input_string,input_lenght)) then
select 'a';
else
select 'b';
end if;

not allowed to return a result set from a function mysql error?

DELIMITER $$
DROP FUNCTION IF EXISTS `workplantype`.`FUN_STOCKINVENTRY_CHECK` $$
CREATE FUNCTION `workplantype`.`FUN_STOCKINVENTRY_CHECK` (
PONo1 VARCHAR(20),
PartCode1 VARCHAR(45)
) RETURNS bool
BEGIN
DECLARE diff bool;
set diff=false;
select if(Remaining_Quantity=0.00, true, false) as diff from tblstockinventory where PONo=PONo1 && PartCode=PartCode1;
return diff;
END $$
DELIMITER ;
how ro avoid the not allowed to return a result set from a function mysql error?
select if(Remaining_Quantity=0.00, true, false) into #diff
from tblstockinventory
where PONo=PONo1 AND PartCode=PartCode1;
You can reduce IF() to
select Remaining_Quantity=0.00 into #diff
And get the same result