my function is returning nothing - mysql

DELIMITER //
DROP FUNCTION IF EXISTS TotalProductionCost;
CREATE FUNCTION TotalProductionCost(Abuilt int(10),Ucost Decimal(5,2) )
RETURNS Decimal(5,2)
BEGIN
DECLARE TotalCost Decimal(5,2);
SET TotalCost = Abuilt * Ucost;
RETURN TotalCost;
END //
DELIMITER ;
SELECT TotalProductionCost(10,1000) AS TotalCost;
I have created above function when Execute it in mysql workbench it worked fine query executed,but when I called function,it didnt return anything.. query executed suceefully.

The sizing of your decimal is exploding based on your input and output of (5,2).
The following works:
DROP FUNCTION IF EXISTS TotalProductionCost;
DELIMITER $$
CREATE FUNCTION TotalProductionCost(Abuilt int,Ucost DECIMAL(12,2) )
RETURNS Decimal(12,2)
DETERMINISTIC
BEGIN
DECLARE TotalCost Decimal(12,2);
SET TotalCost = Abuilt * Ucost;
RETURN (TotalCost);
END $$
DELIMITER ;
test:
SELECT TotalProductionCost(10,1000.12) AS TotalCost;
So, yes, I confirmed yours choked. And you need to be careful with your sizing for the in and return.

Related

error trying to make function for counting friends

Error: There was an error while applying the SQL script to the database.
code:
USE `my_first_db`;
DROP function IF EXISTS `show_users_firends_count`;
DELIMITER $$
USE `my_first_db`$$
CREATE FUNCTION `show_users_firends_count` (ff int)
RETURNS int(11)
BEGIN
Select
count(user_friend.user_id + user_friend.friend_id)
From
user_friend
Where
user_friend.user_id = ff;
END$$
DELIMITER ;
You had it almost right.
A functions ahs to return something so you send the count back.
You should check the query anyway. your count doesn't look right. because you add useid and freind_id and count that, that is wrong. so check the query and change it. it now count all friends_id that are connected to a user_id
DROP function IF EXISTS `show_users_friends_count`;
DELIMITER $$
CREATE FUNCTION `show_users_friends_count` (ff int)
RETURNS int
DETERMINISTIC
BEGIN
Select
count(user_friend.friend_id) INTO #count
From
user_friend
Where
user_friend.user_id = ff;
RETURN #count;
END$$
DELIMITER ;

How to set varchar datatype in function?

I have an issue in my code where i took this : (ApliEMAIL int) in below code but when i execute function it return empty values because in table i took email filed as varchar.
When i write code with this (ApliEMAIL varchar) it does not create function and gives error.
DELIMITER $$
#DROP FUNCTION IF EXISTS `get_appli_name`$$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL int)
RETURNS VARCHAR(255)
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
DELIMITER ;
DELIMITER $$
#DROP FUNCTION IF EXISTS `get_appli_name`$$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar)
RETURNS VARCHAR(255)
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
DELIMITER ;
I tested your function. I got this error.
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
By the way, in the future, when you are asking for help with an error, include the error message in your post. Help us to help you! Don't make us guess!
Read the documentation about READS SQL DATA here: https://dev.mysql.com/doc/refman/8.0/en/stored-programs-logging.html
So I added that option to your function:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
It works now:
mysql> select get_appli_name_by_email('thefrog#muppets.org');
+------------------------------------------------+
| get_appli_name_by_email('thefrog#muppets.org') |
+------------------------------------------------+
| Kermit |
+------------------------------------------------+
You are missing the length of the ApliEMAIL-parameter.
Insted of:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar)
you should have:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar(255))
..or whatever the length of ApliEMAIL is.
You must add deterministic or reads sql data flag.
DELIMITER $$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar(50))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
RETURN ifnull((SELECT apli_fname FROM tbl_signup WHERE apli_email = ApliEMAIL LIMIT 1), '');
END$$
DELIMITER ;

Create function that returns a "SELECT" statement result

I was told to create a mysql function that, given the artwork title, return the name of the creator. I know it sounds silly, i could use just a select.
I followed some tutorials, but none of them helped me. When I try to create the function, mysql workbench says "Error Code: 1415. Not allowed to return a result set from a function"
Here is what i'm trying to do
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
SELECT result= ar.artistName FROM artist as ar, art_object as oa
where ar.artistCode= oa.artistCode
and oa.title= workName
group by ar.artistName ;
return result;
end$$
delimiter ;
That's not how you assign the result of a variable in a SELECT statement. You have to use SELECT ... INTO variable.
You also need to declare the variable first.
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
DECLARE result VARCHAR(250);
SELECT DISTINCT ar.artistName FROM artist as ar, art_object as oa
INTO result
where ar.artistCode= oa.artistCode
and oa.title= workName;
return result;
end$$
delimiter ;
SELECT result = ar.artistName is comparing the value of result with ar.artistName, and returning the result of that comparison from the SELECT statement.

MySQL ERROR #1415 - Not allowed to return a result set from a function

My question is about a function returning only one value but I still get this error, so What I'm supposed to get is average day between the order date and the shipped date, the query is doing that and returning me only one value which is the average. If I use just the SELECT statement outside the of the function I get one column/row answer of 8.4920.
How can I fix that please. Thanks.
DELIMITER //
DROP FUNCTION IF EXISTS OrderFulfilmel//
CREATE FUNCTION OrderFulfilmel(average int) RETURNS DOUBLE Deterministic
BEGIN
SELECT AVG(DATEDIFF(ShippedDate, OrderDate)) AS averageDay
FROM Orders;
END//
DELIMITER ;
You can try below
CREATE FUNCTION OrderFulfilmel(average int) RETURNS DOUBLE Deterministic
BEGIN
DECLARE var_name DECIMAL(10,2);
SET var_name = 0;
SELECT AVG(DATEDIFF(ShippedDate, OrderDate)) INTO var_name
FROM Orders;
RETURN var_name;
END
I don't understand why your function would have an argument. So, I'm thinking:
DELIMITER //
DROP FUNCTION IF EXISTS OrderFulfilmel//
CREATE FUNCTION OrderFulfilmel ()
RETURNS DOUBLE Deterministic
BEGIN
DECLARE #diff DOUBLE;
SELECT #diff := AVG(DATEDIFF(ShippedDate, OrderDate)) AS averageDay
FROM Orders;
RETURN #diff;
END//
DELIMITER ;

mysql function if/else

I have a function that returns a date string. I need this because I can't use a variable in a view, but I can use a function that returns a variable that I set ahead of time...
So I got all that working, but then I decided that if that I wanted it to return the current date if no date variable was set. I thought the code below wold work, but I get syntax errors...
DELIMITER $$
USE `cc`$$
DROP FUNCTION IF EXISTS `ox_date`$$
CREATE FUNCTION `ox_date`() RETURNS CHAR(50) CHARSET latin1
DECLARE ox VARCHAR(20)
IF #oxdate <1 THEN SET ox = CURDATE$$
ELSE SET ox = #oxdate$$
RETURN ox $$
DELIMITER ;
I tried isnull on that first if, but it wasn't any help.
I'm no expert but here are a few of things I see.
First, you've got
DELIMITER $$
and then use it in the function itself. That DELIMITER line allows you to use the semicolons within the body of the function. Otherwise the ';' would end the CREATE FUNCTION statement prematurely.
Also, the line
DECLARE ox varchar(20)
is missing a semicolon at the end.
And then you're missing the
END IF;
after the else condition.
Also what about the BEGIN END$$ wrapped around the function's definition?
I'd expect a stored function to generally take the form:
DELIMITER $$
DROP FUNCTION IF EXISTS `testdb`.MyFunc$$
CREATE FUNCTION `testdb`.`MyFunc` () RETURNS INT
BEGIN
DECLARE someVar varchar(20);
# some stuff
RETURN something;
END $$
DELIMITER ;
Modifying the guts of the function to suit your needs and setting the return type as appropriate.
Anyway, I'm not an expert but that is what I see and hope that helps.
Why do you need to fiddle with the delimiter? For simple logic prefer the IF function to the IF statement.
CREATE FUNCTION `ox_date`( )
RETURNS CHAR(50) CHARSET latin1
RETURN IF(#oxdate < 1 OR #oxdate IS NULL, CURDATE(),#oxdate)
Else
DELIMITER $$
USE `cc`$$
DROP FUNCTION IF EXISTS `ox_date`$$
CREATE FUNCTION `ox_date`( )
RETURNS CHAR(50) CHARSET latin1
RETURN IF(#oxdate < 1 OR #oxdate IS NULL, CURDATE(),#oxdate)$$
DELIMITER ;