Can a stored procedure/function return a table?

前端 未结 5 1093
名媛妹妹
名媛妹妹 2020-12-01 06:14

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

Creating the following procedure

CREATE PROCEDURE database.getE         


        
相关标签:
5条回答
  • 2020-12-01 06:50

    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 讨论(0)
  • 2020-12-01 06:51

    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
    
    0 讨论(0)
  • 2020-12-01 07:08

    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.

    0 讨论(0)
  • 2020-12-01 07:08

    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

    0 讨论(0)
  • 2020-12-01 07:15

    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.

    0 讨论(0)
提交回复
热议问题