Cambiata Cambiata - 1 year ago 79
MySQL Question

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?

Answer Source

As for now, this is not possible.

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

    table_reference [, table_reference] ...

  | join_table

    tbl_name [[AS] alias] [index_hint)]
  | table_subquery [AS] alias
  | ( table_references )
  | { OJ table_reference LEFT OUTER JOIN table_reference
        ON conditional_expr }

    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

    ON conditional_expr
  | USING (column_list)

    USE {INDEX|KEY} [FOR JOIN] (index_list)
  | IGNORE {INDEX|KEY} [FOR JOIN] (index_list)
  | FORCE {INDEX|KEY} [FOR JOIN] (index_list)

    index_name [, index_name] ...

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download