Error :write function in mysql - mysql

I have written a mysql function to check duplicate values in table. But instead return result of function, it returns number of rows in table. I don't know why and how to solve this problem. Please help me
here is my mysql funcion:
CREATE DEFINER=`root`#`localhost` FUNCTION `check_duplicate`(tenMH varchar(45),
thongtin varchar(45),mausac varchar(45),
kichco varchar(45),giatran double,mancc int,madm int) RETURNS int(11)
BEGIN
declare result int default 0;
select count(*) into result from phpdb.mathang where (TenMH=tenMH) and (ThongTin=thongtin)
and(MauSac=mausac) and(KichCo=kichco) and(GiaTran=giatran)and(MaNCC=mancc) and(MaDM=madm);
RETURN result;
END

Your function returned exactly what it should do.
If you want to fetch those duplicate records you better go for a PROCEDURE but not a FUNCTION.
Functions are to return a single row, single column results.
Procedures can be defined to perform some operations and optionally return multi value and multi row outputs.
Convert your function to a procedure so that you can capture duplicate records as a resultset.
And as a precaution, input parameter names should have different naming convention than the table columns for restricting the name ambiguity.
Your procedure should be looking as follows:
drop procedure if exists get_duplicates;
delimiter //
create definer=`root`#`localhost`
procedure `get_duplicates`(
in _tenmh varchar(45),
in _thongtin varchar(45),
in _mausac varchar(45),
in _kichco varchar(45),
in _giatran double,
in _mancc int,
in _madm int )
begin
select * from phpdb.mathang
where tenmh = _tenmh
and thongtin = _thongtin
and mausac = _mausac
and kichco = _kichco
and giatran = _giatran
and mancc = _mancc
and madm = _madm;
end;
//
delimiter ;
Refer to Documentation:
CREATE PROCEDURE and CREATE FUNCTION Syntax

Related

Ask how to pass in a parameter that returns the value of 2 columns in a table in the mysql function

DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `sumwhere`(`mus_level` INT) RETURNS int(11)
READS SQL DATA
begin
declare tong int;
select sum(us_id) into tong
from members
where us_level = mus_level
group by us_level;
return tong;
end$$
DELIMITER ;
CREATE FUNCTION FUNC_1_MANV
(
#MANV CHAR(10)
)
RETURNS #TABLE TABLE(HOTEN NVARCHAR(30), CHUCVU NVARCHAR(30))
AS
BEGIN
INSERT INTO #TABLE
SELECT TENNV , CHUCVU
FROM NHANVIEN
WHERE MANV=#MANV
RETURN
END
GO
--THỰC THI
SELECT * FROM dbo.FUNC_1_MANV('NV001')
GO
enter image description here
I want to do the same as below code func "FUNC_1_MANV" but using MySQL.
I want to display one more column of mine, like count(us_id) , or the value of a column (us_id) when I have where run on create function MySQL how. I see MySQL only return 1 parameter.

MySQL: Function not returning the correct integer

We have a question regarding a function returning the wrong integer-value in MySQL. We have checked that "booked_passengers" contains the right value, 0, and it works just fine when removing that variable, meaning just returning the integer 40. But as soon as we try to subtract "booked_passengers" from it, which still should end up returning 40, it does not work.
Including the code below.
Thanks in advance! :-)
CREATE FUNCTION calculateFreeSeats(flightnumber INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE booked_passengers INT;
SELECT BOOKED_PASSENGERS INTO booked_passengers FROM FLIGHT WHERE (flightnumber = NR);
RETURN (40-booked_passengers);
END $$
When column name and local variable name interfere and there is no table alias then the variable is preferred. So your SELECT BOOKED_PASSENGERS ... selects variable value, not column value. Use
CREATE FUNCTION calculateFreeSeats(flightnumber INT)
RETURNS INT
READS SQL DATA
BEGIN
DECLARE booked_passengers INT;
SELECT FLIGHT.BOOKED_PASSENGERS INTO booked_passengers FROM FLIGHT WHERE (flightnumber = NR);
RETURN (40-booked_passengers);
END $$
From the other side the variable usage is obviously excess:
CREATE FUNCTION calculateFreeSeats(flightnumber INT)
RETURNS INT
READS SQL DATA
RETURN (SELECT 40 - BOOKED_PASSENGERS FROM FLIGHT WHERE flightnumber = NR LIMIT 1);

Call a stored function in mysql,But get an error:Subquery returns more than 1 row

In mysql database,I have a table called 'usertest' which has some user information stored in it. And I create a UDF called 'getId' successfully. This is how it created:
create function getId(name varchar(255))
returns int
begin
declare id_found int;
set id_found=(select Id from usertest where Name=name);
return id_found;
end
It should be OK.However,when I call
select getId('mike');
mysql engine reports an error:
Subquery returns more than 1 row.
But in table usertest I have only one row where Name='mike'.That's wired.Someone can tell me why? Thank you in advance :)
Column names and variables are case-insensitive, so Name and name are the same thing. It's not comparing the Name column with the name variable, it's comparing the name variable with itself, so it's always true.
Use a different name for the function parameter.
create function getId(name_param varchar(255))
returns int
begin
declare id_found int;
set id_found=(select Id from usertest where Name=name_param);
return id_found;
end
or use a table name prefix.
create function getId(name varchar(255))
returns int
begin
declare id_found int;
set id_found=(select Id from usertest where usertest.Name=name);
return id_found;
end
See MySQL : When stored procedure parameter name is the same as table column name

function that returns a table sql

I have this function:
DROP FUNCTION IF EXISTS MinCarAltezza;
DELIMITER $$
CREATE FUNCTION MinCarAltezza(altezza INT)
RETURNS SchedaTab TABLE(
Nome varchar(64),
Cognome varchar(64),
ID_Scheda int(10),
Sequenza int(2),
nSerie int(2),
nRipetizioni int(2),
Carico_Minimo decimal(4,1),
Esercizio varchar(30),
PRIMARY KEY(Nome,Cognome,Esercizio)
)
AS BEGIN
INSERT SchedaTab
SELECT DISTINCT U.Nome,U.Cognome,P.ID_Scheda,P.Sequenza,P.nSerie,
P.nRipetizioni,MIN(P.Carico),P.Esercizio
FROM utente AS U, scheda AS S, programma AS P
WHERE U.CF=S.ID_Utente AND S.ID_Scheda=P.ID_Scheda AND U.Altezza>altezza
AND P.Carico<>0
AND S.ID_Ist NOT IN(SELECT CF FROM istruttore WHERE Stipendio>500)
GROUP BY U.Nome,U.Cognome,S.ID_Scheda
RETURN
END $$
DELIMITER ;
that gives me an error in the line 4 where I declare the return type TABLE.
Is there something I'm missing?
That's the db if someone needs it: http://pastebin.com/DWYqVBpa
thank you
In MySQL there is no "table" data type, hence the error message. As MySQL documentation on stored functions says, return value can be
Any valid MySQL data type
Therefore, a stored function cannot return with a table. I would change the function into a stored procedure and
have a select statement at the end of the procedure (MySQL return the result of the last select)
or create a temporary table with the data and return the name of the temporary table in an out parameter

MySQL Stored Procedure returns 0 in parameters from a SELECT

I created a Stored Procedure in MySQL to return some parameters from a SELECT, but doesn't work.
ever returns 0 in userID and otherID.
Someone can help me?
Thanks
DROP PROCEDURE IF EXISTS sp_XPTO;
DELIMITER ;;
CREATE PROCEDURE sp_XPTO(
OUT userID INT UNSIGNED,
OUT otherID INT UNSIGNED,
IN bookID INT UNSIGNED
)
BEGIN
SELECT userID=E.userID, otherID=E.otherID
FROM Exp as E
WHERE E.bookID = bookID;
END ;;
DELIMITER ;
UPDATED:
Sorry,
After I read a lot, I concluded that the error is in how I call the stored procedure, and Ravinder is correct.
Thanks
I am not sure if your intention is to check the existence of a value in database.
Because, your select statement is doing a comparison but not assignment.
SELECT userID=E.userID, otherID=E.otherID
With the above statement, you may be thinking that value of E.userID is assigned to the OUT parameter userID. But the syntax you used is wrong.
In the above statement, both of the expressions return a boolean. As userID value did not match with E.userID the comparison resulted a false and you saw a 0. And it was the same for the otherID variable.
To assign values to variables you have to use INTO instructional operator.
Example:
SELECT E.userID, E.otherID
INTO userID, otherID
Change your stored procedure as below:
DROP PROCEDURE IF EXISTS sp_XPTO;
DELIMITER //
CREATE PROCEDURE sp_XPTO(
OUT userID INT UNSIGNED,
OUT otherID INT UNSIGNED,
IN bookID INT UNSIGNED
)
BEGIN
SELECT E.userID, E.otherID
INTO userID, otherID
FROM Exp as E
WHERE E.bookID = bookID; -- <- there was a ')'. removed.
END;
//
DELIMITER ;
Call the procedure with desired parameters and read the out parameters.
call sp_XPTO( #userID, #otherID, 234 ); -- where book id is 234
select #userID, #otherID;