0

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
$$

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.