function that returns a table sql - mysql

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

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.

Can't create stored procedure: Syntax error

I want to make a procedure that will insert user in table, but it should also check if user is 18 years old. I'm "doing" it with an if statement (p_datum_rodjenja is birth date of user). I'm not sure if my logic works because I cant create this procedure: when I try to run the query I got the following error:
Syntax error. Error code:1064.
Here is my code:
delimiter #
create procedure dodajKorisnikaProvjera
(
IN p_ime varchar(15),
IN p_prezime varchar(15),
IN p_broj_telefona int,
IN p_datum_rodjenja date,
IN p_broj_vozacke int,
IN p_grad_id int
)
BEGIN
if(TIMESTAMPDIFF(p_datum_rodjenja,CURDATE()) >18) then
INSERT into korisnik(
ime,
prezime,
broj_telefona,
datum_rodjenja,
broj_vozacke,
grad_id)
VALUES(
p_ime,
p_prezime,
p_broj_telefona,
p_datum_rodjenja,
p_broj_vozacke,
p_grad_id
);
else
select'Korisnik mora imati vise od 18 godina.';
end if;
END#
delimiter;
You are having error in Timestampdiff function.
Correct out it like this:
TIMESTAMPDIFF(year,p_datum_rodjenja,CURDATE()) >18)

Error :write function in 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

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;

test a stored procedure in MySql Workbench

I have an Insert stored procedure where I am inserting into 2 tables. The second table using the Last_Insert_ID of the first table. Here is my sproc:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `new_user_create`(
IN oFarmName varchar(45),
IN oFirstName varchar(45),
IN oAddress1 varchar(45),
IN oCity varchar(45),
IN oState varchar(45),
IN oZip varchar(45),
IN oCountry varchar(45)
)
BEGIN
insert into intelliair.individual
( FarmName, FirstName)
values ( oFarmName, oFirstName);
insert into intelliair.address
(IndividualID, Address1, City, State, Zip, Country)
Values (Last_Insert_ID(), oAddress1, oCity, oState, oZip, oCountry);
END
Here is how I am testing the query in MySql workbench:
call new_user_create(#myFarm, #MyName, #MyAddress, #MyCity, #MyState, #MyZip, #MyCountry)
There error I get is: "Column Address1 cannot be null"
Where am I going wronng? Is it in the sproc? Or the way I am calling it?
"Column Address1 cannot be null" indicates that the intelliair.address.Address1 field must be defined not null.
And, I don't think that you pre defined value for #MyAddress before passing it to the stored procedure.
Unless defined it is treated as NULL and hence is the error thrown.
To cross check values before calling the stored procedure like :
select #MyAddress; -- ,#myFarm, #MyName, #MyCity, #MyState, #MyZip, #MyCountry;
Update 1:
You can call stored procedure by directly inputting values for each of the parameters.
Example:
call new_user_create(
'my Farm value', -- #myFarm
'E B', -- #MyName
'My Address is SO', -- #MyAddress1
'My City is Coders', -- #MyCity
'CA', -- #MyState
'12345', -- #MyZip
'US' -- #MyCountry
);
The exception is being thrown by an INSERT (or UPDATE) statement, that is assigning a NULL value to a column named Address1 that is declared to be NOT NULL.
The most likely explanation, from what you show, is that the value passed in as the oAddress1 parameter is NULL, and the exception is being thrown by the second INSERT statement.
The most likely explanation, therefore, is that when the call to the procedure is made, the #MyAddress user variable is not assigned a value.
(You can verify there is not an Address1 column on the intelliair.individual table, or that if there is one, it's not defined as NOT NULL.)
(There's also a possibility that it's not one of your statements that's throwing the exception, but rather a recursive SQL statement, like an INSERT statement in a BEFORE INSERT FOR EACH ROW trigger.)
USE lportal;
DELIMITER $$
CREATE PROCEDURE `iemp`(IN eid INT(11),IN ename varchar(15),IN dname varchar(15),IN doj DATE)
BEGIN
INSERT INTO e_emp (eid,ename,dname,doj) VALUES (eid,ename,dname,doj);
END