简体   繁体   中英

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM