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;
Related
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;
I am trying to create a function in MySQL like so, bu tI am getting a syntax error at the if exists line:
I think I am doing something slightly off as a result of translation from MS SQL server.
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field)
RETURN 'True'
RETURN 'false'
END;
**UPDATE
The solution I found based on the answer from #SK Jajoriya
DELIMITER $$
CREATE FUNCTION MyFunction2(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field) THEN
RETURN 'True';
ELSE
RETURN 'False';
END IF;
END $$
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field) THEN
RETURN 'True'
ELSE
RETURN 'false'
END IF;
END;
In your case, the if statement not closed
IF should be finished with END IF
https://dev.mysql.com/doc/refman/5.7/en/if.html
But in your case it's better to
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
DECLARE res VARCHAR(5);
SET res = IF(EXISTS (SELECT 1 FROM Teaches WHERE courseid = input_field LIMIT 1),'true','false');
RETURN res;
END;
here is a fiddle
https://www.db-fiddle.com/f/uFkXXDpqVjn6AjYkXx3EYM/0
I am trying to create the below function in MySQL but getting syntax error.
I am not able to find the solution, would be grateful for some help
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar
BEGIN
DECLARE title varchar;
if depart_id = 1 then
set title='IT Department';
else if depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END$$
DELIMITER ;
You have several syntax errors in your script:
varchar must have a length
You should define DELIMITER $$ first
It's not else if, but elseif
Try this;)
DELIMITER $$
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar(10)
BEGIN
DECLARE title varchar(10);
if depart_id = 1 then
set title='IT Department';
elseif depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END $$
DELIMITER ;
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
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