Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a stored procedure/function return a table?

Can a MySql stored procedure / function return a table without the use of temp table?

Creating the following procedure

CREATE PROCEDURE database.getExamples()      SELECT * FROM examples; 

and later calling it with

CALL database.getExamples() 

displays the example table - just as expected - but the following doesn't seem to be possible:

SELECT * FROM CALL database.getExamples() 

Is it possible at all to return a query result table from a stored procedure / function, and if so - how?

like image 261
Cambiata Avatar asked Oct 28 '09 13:10

Cambiata


People also ask

Can a stored procedure return table?

The RETURN exits the stored procedure, and nothing that follows it will be executed, including the SELECT statement on the following line. Otherwise, if you want the data for the entire table, as your question shows, add a SELECT after the INSERT . But don't put RETURN in front of it!

Can a stored procedure return a table MySQL?

You cannot return table from MySQL function. The function can return string, integer, char etc. To return table from MySQL, use stored procedure, not function.

What can stored procedures return?

Return Value in SQL Server Stored Procedure In default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.


1 Answers

As for now, this is not possible.

Here is the documentation on what may be used in the FROM clause:

table_references:     table_reference [, table_reference] ...  table_reference:     table_factor   | join_table  table_factor:     tbl_name [[AS] alias] [index_hint)]   | table_subquery [AS] alias   | ( table_references )   | { OJ table_reference LEFT OUTER JOIN table_reference         ON conditional_expr }  join_table:     table_reference [INNER | CROSS] JOIN table_factor [join_condition]   | table_reference STRAIGHT_JOIN table_factor   | table_reference STRAIGHT_JOIN table_factor ON conditional_expr   | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition   | table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor  join_condition:     ON conditional_expr   | USING (column_list)  index_hint:     USE {INDEX|KEY} [FOR JOIN] (index_list)   | IGNORE {INDEX|KEY} [FOR JOIN] (index_list)   | FORCE {INDEX|KEY} [FOR JOIN] (index_list)  index_list:     index_name [, index_name] ... 

As you can see, stored procedures are not in this list.

like image 79
Quassnoi Avatar answered Oct 03 '22 20:10

Quassnoi