No return found in function - mysql

I have browsed around and other solutions don't seem to be working for me here. I keep getting a 'No return found for functon' error when trying to create this function in MySQL. Any idea why?
CREATE FUNCTION `mydb`.`myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
SELECT SUM(Transaction.Bought) INTO #Qty FROM Transaction WHERE Transaction.Name = Name;
RETURN #Qty

Try this
DELIMITER $$
CREATE FUNCTION `myfunction`(`Name` VARCHAR(20) CHARSET utf8) RETURNS INT NOT DETERMINISTIC
READS SQL DATA
MAIN: BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal FROM `Transaction` WHERE `Transaction`.Name = Name;
RETURN returnVal;
END MAIN;$$
DELIMITER ;

Alternatively, try this,
DELIMITER $$
CREATE FUNCTION `myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal
FROM `Transaction`
WHERE `Transaction`.Name = Name;
RETURN returnVal;
END$$
DELIMITER ;

delimiter //
create function myfun1234 (i int,j int)
returns int
begin
declare x int ;
declare y int ;
declare z int ;
set x=10;
set y=20;
if (1<=x && j>=y )then
set z=i+j;
end if;
return z;
end; //
-- error1 FUNCTION myfun12 ended without RETURN

Related

Create mysql function failed in phpmyadmin

When I try to make this function it always failed.
DELIMITER $$
CREATE FUNCTION `f_hadir`(ID INT)
RETURNS INT
BEGIN
DECLARE TMP INT;
SELECT COUNT(ID_presensi_siswa) into TMP
FROM presensi_siswa
WHERE keterangan='Hadir' AND id_siswa=id LIMIT 1;
RETURN TMP;
END$$
DELIMITER ;
i had similiar code but and it successfully created.
DROP FUNCTION IF EXISTS `CategoriesToString`;
DELIMITER ;;
CREATE FUNCTION `CategoriesToString`(TID int) RETURNS varchar(500) CHARSET utf8
BEGIN
DECLARE result varchar(500);
SELECT group_concat(Category.Name) INTO result FROM Category
WHERE ID IN (SELECT CategoryID FROM TopicHasCategory WHERE TopicID = TID) LIMIT 1;
RETURN result;
END;;
DELIMITER ;
Anyone have idea?

mySQL Create Function

Can anyone help work out why this create function isn't working. I've copied it from a a program teaching me mySQL and I've double checked and it doesn't seem to work.
any help greatly appreciated
Delimiter//
Create Function CAP_FIX(Input VARChar(255))
Returns varchar(255)
Deterministic
begin
Declare first Char;
Declare remainder varchar(255);
Set first = substring(input,1,1);
Set remainder = substring(input,2,char_length(input) -1);
Return concat(first, lower(remainder));
end//
Delimiter;
It seems as if you're missing a space after Delimiter
Delimiter //
Create Function FUNCTION_NAME (Input VARChar(255))
Returns varchar(255)
Deterministic
begin
Declare first Char;
Declare remainder varchar(255);
Set first = substring(input,1,1);
Set remainder = substring(input,2,char_length(input) -1);
Return concat(first, lower(remainder));
end//
Delimiter ;

IF statement in MySQL stored function goes wrong

I get a error on line 12 (at the endif statement).
I belief I´m doing something wrong within the IF or ELSE, can anyone help me?
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
IF `param` > 0 THEN
SET `var` = `var` + `param`;
END IF;
RETURN `var`;
END$$
EDIT: (same function with case instead of if, same problem)
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
SET `var` =
CASE
WHEN `param` > 0 THEN `var` + `param` ELSE `var`
END ;
RETURN `var`;
END$$
Try this instead:
BEGIN
DECLARE `var` INT;
SET var =
CASE
WHEN param > 0 THEN var + 1 ELSE var
END ;
RETURN var;
END$$
Found out it was a bug in PHPMyAdmin, if I added the function with 'add routine' it worked!

MYSQL function, is it Workbench or just noobish me?

I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)

What's wrong with this MySQL Stored Function?

Having trouble getting this to apply in MySQL Workbench 5.2.15
DELIMITER //
CREATE
DEFINER=`potts`#`%`
FUNCTION
`potts`.`fn_create_category_test` (test_arg VARCHAR(50))
RETURNS int
BEGIN
DECLARE new_id int;
SET new_id = 8;
RETURN new_id;
END//
The actual function will have a lot more between BEGIN and END but as it stands, even this 3 liner won't work.
Thanks!
DELIMITER $$
CREATE FUNCTION `fn_create_category_test` (test_arg varchar(50))
RETURNS INT
BEGIN
DECLARE new_id int;
set new_id=8;
return new_id;
END $$
DELIMITER ;
Works fine for me, try getting rid of DEFINER?