stored function returns 0 mysql - mysql

I make a this stored function to return the amount of invoices per customer:
delimiter //
CREATE FUNCTION function1(id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = #id);
RETURN #result;
END//
delimiter ;
but when I use it, returns 0:
SELECT function1(12) AS Q;
and the query returns 428 :
SELECT count(invoice_id) AS Q FROM invoices WHERE customer_id = 12;
I need to know what am I doing wrong.

#id is not the same as id
But it id better to use variable names _id to differentiate variables from column names
delimiter //
CREATE FUNCTION function1(_id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = _id);
RETURN #result;
END//
delimiter ;

Related

how to create a function in mySQL workbench

trying to get the total number of boxes ordered from a given type("mytype").
something seems to be wrong with my syntax
create function NumOrdersForBoxType (mytype varchar(20))
returns int
begin
DECLARE #numorders int
select #numOrders = count(*)
from BOXES as B
where B.Type = mytype
return #numOrders
end
;
In mysql you don't declare user defined variables (at variables) stackoverflow.com/questions/11754781/… AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables
delimiter $$
create function f(mytype varchar(20))
returns int
begin
DECLARE numorders int;
set numorders = (select count(*)
from boxes as B
where B.Type = mytype
);
return numOrders;
end $$
delimiter ;

Not allowed to return a result ? stored procedure mysql

CREATE TABLE ZAMOWIENIA( id_zamowienia INT PRIMARY KEY NOT NULL,
id_uzytkownika INT, kwota_zamowienia DECIMAL(10,2));
INSERT INTO
ZAMOWIENIA VALUES (1,1,20), (2,4 ,5), (3,3,100), (4,1,300),
(5,2,80), (6, 1,150);
SELECT * FROM ZAMOWIENIA;
select
count(id_uzytkownika) from ZAMOWIENIA WHERE id_uzytkownika=1;
DROP
FUNCTION AccountType;
DELIMITER //
CREATE FUNCTION AccountType(
in_id_uzytkownika INT) RETURNS VARCHAR(20) DETERMINISTIC
BEGIN
DECLARE account_type VARCHAR(20);
DECLARE in_id_uzytkownika INT;
DECLARE zamowienia INT;
select count(id_uzytkownika) AS zamowienia
from ZAMOWIENIA
WHERE id_uzytkownika=in_id_uzytkownika;
select zamowienia from zamowienia;
IF zamowienia >10 THEN
SET account_type = 'BRONZE';
END IF;
IF zamowienia >30 THEN
SET account_type = 'SILVER';
END IF;
IF zamowienia >60 THEN
SET account_type = 'GOLD';
END IF;
return account_type;
END // DELIMITER ;
If you want to set a variable to the result of a SELECT query, you need to use SELECT ... INTO variable, not SELECT ... AS variable. Your function is trying to return the result of the SELECT query because it doesn't store the result into a variable.
Or you can use SET statement, which is simpler when you're just setting a single variable.
So replace the two SELECT statements with:
SET zamowienia = (SELECT COUNT(*) FROM from ZAMOWIENIA
WHERE id_uzytkownika=in_id_uzytkownika);
Also, you shouldn't have DECLARE statements for the function parameters. Remove the line
DECLARE in_id_uzytkownika INT;

mysql function parameter not work in where condition of select statement

DELIMITER $$
CREATE DEFINER=`axistms`#`localhost` FUNCTION `CheckDoc`(`orderId` INT) RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lvl int;
SELECT count(`id`) INTO lvl FROM `com_carrier_portal_upload_documents` WHERE `orderID`= orderId;
RETURN lvl;
END$$
DELIMITER ;
any order id pass through orderId parameter its doesn't effect on where condition. Always return count of all records.How to fix this?
I'd suggest you to rename stored procedure argument, do something like this -
CREATE DEFINER = `axistms`#`localhost` FUNCTION `CheckDoc` (orderIdParam int)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE lvl int;
SELECT
COUNT(`id`) INTO lvl
FROM `com_carrier_portal_upload_documents`
WHERE `orderID` = orderIdParam;
RETURN lvl;
END
...because WHERE orderID= orderId can be equal to WHERE true.

Wrong result while declaring a variable inside Stored procedure

Following is a simple stored procedure to calculate male count from a table , I have declared a variable total_count inside the proc where i'm storing my result.
DELIMITER //
CREATE PROCEDURE GetMaleCount()
BEGIN
DECLARE total_count INT DEFAULT 0 ;
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount();
select #total_count as tc;
When i executed this procedure i'm getting NULL as the answer, but when i seperately executed just the inner sql query i got the right answer 1852. have i declared the variable in the wrong way ?
total_count that you've declared is visible only in procedure. That is why it is NULL outside of it. You need to use OUT parameter when defining procedure:
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;
You need to use OUT parameter.
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;

MySQL Functions Didn't return any value

I trying to write MySql Function to return the count of child for one node and the I wrote this code
CREATE DEFINER=`root`#`localhost` FUNCTION `get_child_count`(`id` INT) RETURNS int(11)
READS SQL DATA
Begin
declare temp int(11);
SELECT count(*) into temp FROM tbl_photo_album where parent_id = id;
return temp;
End
and everytime I tried to run this function it come with nothing.
Possible alternative: Instad of select ... into..., try this:
set temp = (select count(*) from tbl_photo_album where parent_id = id);
try this one.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `get_child_count`(`id` INT) RETURNS int(11)
READS SQL DATA
Begin
declare temp int(11);
SELECT count(*) into temp FROM tbl_photo_album where parent_id = id;
return temp;
END$$
DELIMITER ;
select get_child_count(10); -- Pass Id for what you want result.