MYSQL Error while creating a function - mysql

I have a function
DELIMITER $$
CREATE FUNCTION `GetSuppliedMedicineQuantity` (FromDate varchar(50),Phid int,Todate varchar(50), MId INT)
RETURNS INTEGER
BEGIN
DECLARE totalmedicinecount INT;
IF(Todate is null)
then
select totalmedicinecount=sum(Totalquantity+FreeMedQuantity) from suppliedmedicine where
DATE(DateOfSupply)>=DATE(FromDate)
and Pharmacyid=phid and Medicineid=MId
group by Medicineid;
else
select totalmedicinecount=sum(Totalquantity+FreeMedQuantity) from suppliedmedicine where
DATE(DateOfSupply)<DATE(FromDate) and DATE(dateofsupply)<DATE(Todate)
and Pharmacyid=phid and Medicineid=MId
group by Medicineid;
end if;
SET totalmedicinecount=IF(ISNULL(totalmedicinecount),0,totalmedicinecount);
RETURN totalmedicinecount;
END$$
While creating the function I am getting an error :
ERROR 1415: Not allowed to return a result set from a function
I am very new to MYSQL and don't know how to solve this, kindly help. Thanks in advance.

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 ;

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 ;

Error Code: 1305 MySQL, Function does not Exist

I have a problem. I created a function in MySQL which returns a String (varchar data type).
Here's the syntax:
DELIMITER $$
USE `inv_sbmanis`$$
DROP FUNCTION IF EXISTS `SafetyStockChecker`$$
CREATE DEFINER=`root`#`localhost` FUNCTION `SafetyStockChecker`
(jumlah INT, safetystock INT)
RETURNS VARCHAR(10) CHARSET latin1
BEGIN
DECLARE statbarang VARCHAR(10);
IF jumlah > safetystock THEN SET statbarang = "Stabil";
ELSEIF jumlah = safetystock THEN SET statbarang = "Perhatian";
ELSE SET statbarang = "Kritis";
END IF;
RETURN (statbarang);
END$$
DELIMITER ;
When I call the function like call SafetyStockChecker(16,16), I get this error:
Query : call SafetyStockChecker(16,16)
Error Code : 1305
PROCEDURE inv_sbmanis.SafetyStockChecker does not exist
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
What's wrong with the function?
That is not the correct way to call a function. Here's an example to call a function:
SELECT SafetyStockChecker(16,16) FROM TableName
The way you are doing now is for calling a STORED PROCEDURE. That is why the error says:
PROCEDURE inv_sbmanis.SafetyStockChecker does not exist
because it is searching for a Stored procedure and not a function.
You should use
SELECT SafetyStockChecker(16,16)

Mysql stored function freezing

I have a stored function in MySQL and it works partially.
DELIMITER $$
DROP FUNCTION IF EXISTS `getsubdomain`$$
CREATE FUNCTION getsubdomain(page_id int(11))
RETURNS CHAR(255)
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
declare current_p_id int(11);
declare current_p_parent_id int(11);
declare current_p_address_type char(255);
declare current_p_adress char(255);
SET current_p_id = page_id;
WHILE (current_p_id) <> 0
DO
select p_parent_id, p_address_type, p_adress from opu_pages where p_id = current_p_id into current_p_parent_id, current_p_address_type, current_p_adress;
IF current_p_address_type <> ''
THEN
IF current_p_address_type = 'subdomain'
THEN
RETURN current_p_adress;
ELSE
SET current_p_id = current_p_parent_id;
END IF;
ELSE
RETURN NULL;
END IF;
END WHILE;
RETURN NULL;
END$$
DELIMITER ;
If I call in query SELECT getsubdomain(p_id) FROM opu_pages; it works Ok. But if I call it in SELECT * FROM opu_pages WHERE getsubdomain(p_id)='library'; the database is collapsed and freezing.
Query and function work with one table.
What did I do wrong?
I thought that it can be caused by the table format MyISAM. But I can't change it to InnoDB because I use FULLTEXTFORMAT fields in this table.
Table opu_pages (MyISAM) scheme
p_id INT
p_parent_id INT
p_address_type ENUM (path, subdomain)
p_adress VARCHAR
Based on your post I would say that your code is entering an infinite loop for some of your input parameters.
In particular the case where p_id = p_parent_id in the opu_pages table and the current_p_address_type = 'subdomain'