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
Related
I have an issue where I'm trying to make a stored function to search for a user's username and password. The arguments to the function should be the username and password and it must return true if the username and password combo exist and false if it doesn't. This is what I've created so far with no success:
delimiter $$
create function cred(u varchar(15), p varchar(6))
returns char(5)
begin
declare a CHAR(5);
if (select username = u and pwd = p from customers) then set a = 'true';
else set a = 'false';
end if;
end$$
delimiter ;
select cred('dJete', 'abc112');
You need to use return-statement in function to return the value. Also, minor fixes to the actual query where you match the customer:
create function cred(in_username varchar(15), in_password varchar(6))
returns char(5)
begin
declare v_cnt int;
select count(*) into v_cnt
from customers
where username = in_username and pwd = in_password;
if (v_cnt>0) then
return 'true';
else
return 'false';
end if;
end
$$
create procedure temp (in empId int)
begin
declare emptype varchar;
select emptype = qoute(emptype) from dms_document where id = empid;
select emptype
case
when emptype = 'P' then
select doctype from dms_report where pilot = 1
else
select 'No Documents required'
end case
end;
This is my query i am creating procedure in MySQL, i am getting error in case statement please hlep me why this error is coming how declare case statement why error is coming in workbench for creating procdure
You are missed comma.
CREATE PROCEDURE temp (IN empId INT)
BEGIN
DECLARE emptype VARCHAR;
SELECT emptype = qoute(emptype) FROM dms_document WHERE id = empid;
SELECT emptype,
CASE
WHEN emptype = 'P' THEN doctype;
ELSE 'No Documents required';
END CASE ;
FROM dms_report WHERE pilot = 1
End;
Here I formatted your procedure Use delimiter
DELIMITER //
CREATE PROCEDURE temp ( empId INT)
BEGIN
DECLARE var_etype VARCHAR(36);
SELECT
emptype = QOUTE(emptype)
FROM
dms_document
WHERE
id = empid;
SELECT
emptype,
CASE
WHEN emptype = 'P' THEN doctype
ELSE 'No Documents required'
END
FROM
dms_report
WHERE
pilot = 1;
End//
DELIMITER ;
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;
I'm using phpmyadmin and MySQL to run a simple query, that creates a function checking the existence of a certain record. It keeps throwing a syntax error at line 7 with Declare. I have no idea why. I did try to use the built-in function creator, but it's messed up and I don't like it. Any help appreciated!
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END
Try the following:
DELIMITER $$
DROP FUNCTION IF EXISTS `check_if_card_exists`$$
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END$$
DELIMITER ;
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