Hi I try to write a function in MySQL, which gives me back a value from a table.
I'm getting the error:
Error Code: 1415. Not allowed to return a result set from a function
I know what it means, but I don't know how to solve the problem. I just need one value, the Id of the inserted row. I'm glad for any help.
What the function does is:
Insert a new row into the aip_request table, with a Random value, that way I can identify the row after the insert. The function should return the Id of the created line.
The request_id is an autovalue.
DROP FUNCTION IF EXISTS `sp_get_new_request_id`;
DELIMITER $$
CREATE FUNCTION `sp_get_new_request_id` ()
RETURNS BIGINT
BEGIN
DECLARE var_random bigint;
SET #random := CAST(RAND(NOW()) AS CHAR(150));
INSERT INTO aip_request
(Firstname)
VALUES( #random );
SELECT request_id INTO var_random FROM aip_request
WHERE Firstname = #random
LIMIT 1;
Return var_random;
END
$$
Change this query -
SELECT #random := CAST(RAND(NOW()) AS CHAR(150));
with this one -
SET #random := CAST(RAND(NOW()) AS CHAR(150));
...you cannot execute SELECT query that returns data-set from the stored function.
Related
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.
CREATE FUNCTION `ConvertDate`(StringDate varchar(15))
RETURNS datetime
BEGIN
declare CDATE varchar(10);
SET CDATE = StringDate;
select str_to_date(CDATE,'%Y%m%d %h%i');
RETURN CDATE;
END
I get an error:
Error Code: 1415 Not allowed to return a result set from a function
How can I fix this Error?
Your problem is that your SELECT statement has no INTO clause, so it attempts to return the result set from the function. You could alter that to:
SELECT STR_TO_DATE(CDATE,'%Y%m%d %h%i') INTO CDATE;
But you might as well do all the computation in the RETURN statement:
CREATE FUNCTION `ConvertDate2`(StringDate varchar(15))
RETURNS datetime
RETURN STR_TO_DATE(StringDate,'%Y%m%d %h%i');
You should
Select INTO cdate...
or don't bother with the select and
set cdate = str_to_date..
BUT Unless this is a very simplified version of your actual function I don's see the point of the function.
I'm facing a problem with SP in mySql when I want to select my identete it gives always null as value.
DELIMITER $$
CREATE PROCEDURE insert_details_facture(IN typefacture INT,
IN codeactivite VARCHAR(255),
IN qte INT,
IN pu DOUBLE,
IN unite VARCHAR(255),
IN montant DOUBLE)
BEGIN
DECLARE identete INT;
SELECT identete = numfacture FROM entetefacture WHERE numfacture = LAST_INSERT_ID();
END$$
When I execute this it gives identite = numfacture as column's name and null as value.
CALL insert_details_facture(10,'l',10,12,'l',20)
Check Here.
With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement.
so result you are getting is obvious.
to get last record you can use limit.
SET identete = (SELECT numfacture FROM entetefacture ORDER BY id DESC LIMIT 1);
LAST_INSERT_ID() is not guaranteed to have a valid value. For instance, the documentation states:
If the previous statement returned an error, the value of
LAST_INSERT_ID() is undefined. For transactional tables, if the
statement is rolled back due to an error, the value of
LAST_INSERT_ID() is left undefined.
Similarly, if there have been no database changes for the current session, then the value will be undefined.
Also, if you want to set the value, then use := in a select:
SELECT identete := numfacture
FROM entetefacture
WHERE numfacture = LAST_INSERT_ID();
Why not just get the last row in the table using limit?
SET identete = (SELECT numfacture FROM entetefacture ORDER BY id LIMIT 1);
I have the following stored procedure:
DELIMITER //
CREATE PROCEDURE NewSeqType(IN mySubschemaID INT, IN hashVal bigint(20))
BEGIN
DECLARE newSeqTypeID INT;
SELECT MAX(ID)+1 INTO newSeqTypeID FROM sequenceType WHERE subschemaID=mySubschemaID;
INSERT INTO SequenceType(ID, HashValue, subschemaID) VALUES(newSeqTypeID, hashVal, mySubschemaID);
SELECT LAST_INSERT_ID() as ID; -- return prim key
END//
This works when there is already data in the table where subschemaID=mySubschemaID, but if that SELECT statement returns null, then the MAX(ID)+1 part gives the error column ID cannot be null.
How can I give ID a default value, say 0, in that case?
For this, you can use coalesce():
SELECT coalesce(MAX(ID)+1, 1) INTO newSeqTypeID FROM sequenceType WHERE subschemaID=mySubschemaID;
INSERT INTO SequenceType(ID, HashValue, subschemaID) VALUES(newSeqTypeID, hashVal, mySubschemaID);
Often, this type of work is done in a before insert trigger, to keep the values aligned.
So, I'm making my first MySQL function.
BEGIN
DECLARE ID INT;
SELECT
LandID INTO ID
FROM
Landen
WHERE
Landnaam = landnaam;
RETURN ID;
END
The parameter is a varchar.
So, let's say I got a record in Landen with the Landnaam 'Nederland'.
I run my function, give 'Nederland' as parameter.
But then it gives me to following error:
1172 - result consisted of more than one row
Which makes no sense at all. Cause when I give something as a parameter that is not in my database, like 'ASDASD', I get the same error.
And when limmiting the results with
LIMIT 1
It just always returns 1.
What am I doing wrong?
drop function if exists myfunc;
delimiter //
create function myfunc(str varchar(50))
returns int
reads sql data
begin
declare id int;
select landid into id from Landen where landnaam = str limit 1;
return id;
end//
delimiter ;
select myfunc('nederland');