14
CREATE FUNCTION myFunction(id INT) RETURNS TABLE  
BEGIN  
   RETURN SELECT * FROM board;  
END  

This query gives following error:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE  
1
  • Instead return json Commented Nov 14 at 7:43

2 Answers 2

45

As per documentation on loadable user defined functions in MySQL
you can only return values of type {STRING|INTEGER|REAL|DECIMAL}

CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL|DECIMAL}
    SONAME shared_library_name

If you want to read a select resultset you have to define a procedure but not function.

DELIMITER //

DROP PROCEDURE IF EXISTS myProcedure //

CREATE PROCEDURE 
  myProcedure( id INT )
BEGIN  
   SELECT * FROM board
     -- add where condition if required
    WHERE Col_name = id
   ;  
END 
//

DELIMITER ;

And you can call procedure like

call myProcedure( 6 )

That returns implicit objects based on the statements used in the procedure.

Also Refer to: Adding a Loadable Function

  • Functions can return string, integer, or real values and can accept arguments of those same types
Sign up to request clarification or add additional context in comments.

3 Comments

CREATE PROCEDURE myProcedure( id INT ) BEGIN SELECT * FROM board WHERE id = id ; END //check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
CREATE PROCEDURE myProcedure( id INT ) BEGIN select * from board; END // check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
As we cannot use the result of SP in another SP, so there is no way if you want to do some calculation before returning table and then use the result in another SP?
0

Stored procedures can, but stored functions cannot. If you perform an ordinary SELECT inside a stored procedure, the result set is returned directly to the client. You need to use the MySQL 4.1 (or higher) client/server protocol for this to work. This means that, for example, in PHP, you need to use the mysqli extension rather than the old mysql extension.

reference link: mySql docs

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.