开发者

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

开发者_JAVA技巧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?


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.


According to A.4 MySQL 5.6 FAQ: Stored Procedures and Functions:

A.4.14: Can MySQL 5.6 stored routines return result sets?

Stored procedures can, but stored functions cannot.


It looks like it can be done, but with use of output variables from the stored procedure. http://www.sqlinfo.net/mysql/mysql_stored_procedure_SELECT.php

-- 1. Create Procedure
DROP PROCEDURE IF EXISTS `sp_students_SELECT_byPK` 
GO

CREATE PROCEDURE sp_students_SELECT_byPK
(
    IN   p_student_id                    INT(11)       , 
    OUT  p_password                      VARCHAR(15)   , 
    OUT  p_active_flg                    TINYINT(4)    , 
    OUT  p_lastname                      VARCHAR(30)   , 
    OUT  p_firstname                     VARCHAR(20)   , 
    OUT  p_gender_code                   VARCHAR(1)    , 
    OUT  p_birth_dttm                    DATETIME      
 )
BEGIN 

SELECT password                      , 
       active_flg                    , 
       lastname                      , 
       firstname                     , 
       gender_code                   , 
       birth_dttm                    
INTO   p_password                      , 
       p_active_flg                    , 
       p_lastname                      , 
       p_firstname                     , 
       p_gender_code                   , 
       p_birth_dttm                    
FROM   students
WHERE  student_id = p_student_id ; 

END 

GO

-- 2. Select Results from Stored Procedure
/***
IN    p_student_id   INT(11)
OUT   p_password     VARCHAR(15)
OUT   p_active_flg   TINYINT(4)
OUT   p_lastname     VARCHAR(30)
OUT   p_firstname    VARCHAR(20)
OUT   p_gender_code  VARCHAR(1)
OUT   p_birth_dttm   DATETIME
***/

CALL sp_students_SELECT_byPK
(
  8, 
  @p_password , 
  @p_active_flg , 
  @p_lastname , 
  @p_firstname , 
  @p_gender_code , 
  @p_birth_dttm
)
GO

SELECT @p_password      AS p_password      , 
   @p_active_flg    AS p_active_flg    , 
   @p_lastname      AS p_lastname      , 
   @p_firstname     AS p_firstname     , 
   @p_gender_code   AS p_gender_code   , 
   @p_birth_dttm    AS p_birth_dttm   
GO


Each SELECT statement that does not insert into a table or a variable will produce a result set.

If you want your stored procedure to return only one result set, make sure that you only have one SELECT statement. If you have other SELECT statements, make sure that they insert results into a table or variable.

UPDATE Here are examples of stored procedures.

This stored procedure would return one result set:

DELIMITER ;;
CREATE DEFINER=CURRENT_USER PROCEDURE stored_procedure_name()
BEGIN
    DECLARE local_variable_name INT;

    SELECT column_name FROM table_1 LIMIT 1 INTO local_variable_name;

    SELECT * FROM table_1;
END;;
DELIMITER ;
This stored procedure would return two result sets:

DELIMITER ;;
CREATE DEFINER=CURRENT_USER PROCEDURE stored_procedure_name()
BEGIN
    DECLARE local_variable_name INT;

    SELECT column_name FROM table_1 LIMIT 1 INTO local_variable_name;

    SELECT * FROM table_1;

    SELECT * FROM table_2;
END;;
DELIMITER ;

Ref:https://dba.stackexchange.com/questions/8291/how-does-mysql-return-a-result-set-from-a-stored-procedure


You might be able to do what you are attempting by using a view instead of a stored procedure, but that entirely depends on what the stored procedure does.

If using a temp table is your only option, consider using the MEMORY storage engine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜