title: Return value from Function in MySQL
body: <p>Hi I try to write a function in MySQL, which gives me back a value from a table. </p>
  
  <p>I'm getting the error:</p>
  
  <blockquote>
    <p>Error Code: 1415. Not allowed to return a result set from a function</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>What the function does is:<br>
  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.</p>
  
  <pre><code>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
  $$
  </code></pre>