I have a table named 'reserves' and the exercise asks me to make a function that counts how many reservations are with an id lower than the id I give as the only parameter of the function. They also ask me that when I introduce 'NULL' as a parameter, the function must make a sum of all of the imports from the column 'import' and show the total. I have half of the function working. The 'NULL' part is the one not working.
Now I have this:
CREATE OR REPLACE FUNCTION compta_reserves(int) RETURNS int AS $$
DECLARE
num_reserves INT:=(SELECT COUNT(*) FROM mobilitat.reserves);
id ALIAS FOR $1;
comptador INT:=0;
i INT:=1;
import_total double precision:=((SELECT SUM(import) FROM mobilitat.reserves WHERE import<60)::INT);
BEGIN
IF (id=NULL) THEN
RETURN import_total AS int;
ELSE
WHILE i<num_reserves LOOP
IF (SELECT id_reserva FROM mobilitat.reserves WHERE id_reserva=i)<id THEN
IF (SELECT import FROM mobilitat.reserves WHERE id_reserva=i)<60 THEN
comptador=comptador+1;
END IF;
END IF;
i=i+1;
END LOOP;
RETURN comptador;
END IF;
END;
$$ LANGUAGE PLPGSQL;
When I run the function with the 'NULL' parameter, I just get an empty table with no sum.
Related
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.
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 ;
This Stored Procedure should be returning a false if the value passed in the parameter does not exist in the table.
CREATE DEFINER=`listsjag_user`#`%` PROCEDURE `Select_ReferenceAvailability`(IN ref VARCHAR(45))
BEGIN
IF EXISTS (
SELECT * FROM tbl_dataLibrary
WHERE itemreference = #ref
) THEN
SELECT 'true';
ELSE
SELECT 'false';
END IF;
END
It is returning true for all values but there is only one value in the column itemreference.
Does anyone have any ideas?
A MySQL stored procedure cannot really return a value. You can either pass in an OUT parameter and modify it, or convert your procedure to a function.
CREATE FUNCTION ReferenceAvailability (IN ref VARCHAR(45))
RETURNS boolean
BEGIN
IF(EXISTS(SELECT * FROM tbl_dataLibrary WHERE itemreference = #ref)) THEN
RETURN 1;
END IF;
RETURN 0;
END;
I changed your true/false to 1/0, feel free to change back, but you will need to return varchar(5).
Use the function in a select like this:
SELECT ReferenceAvailability(...)
Hope that helps.
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.
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'