简体   繁体   English

"存储过程\/函数可以返回表吗?"

[英]Can a stored procedure/function return a table?

Can a MySql stored procedure \/ function return a table without the use of temp table? MySql 存储过程\/函数可以在不使用临时表的情况下返回一个表吗?

Creating the following procedure创建以下过程

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

As for now, this is not possible.就目前而言,这是不可能的。

Here is the documentation<\/strong><\/a> on what may be used in the FROM<\/code> clause:以下是有关FROM<\/code>子句中可能使用的内容的文档<\/strong><\/a>:

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] ...

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<\/a> http:\/\/www.sqlinfo.net\/mysql\/mysql_stored_procedure_SELECT.php<\/a>

-- 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.每个不插入表或变量的 SELECT 语句都会产生一个结果集。

If you want your stored procedure to return only one result set, make sure that you only have one SELECT statement.如果您希望您的存储过程只返回一个结果集,请确保您只有一个 SELECT 语句。 If you have other SELECT statements, make sure that they insert results into a table or variable.如果您有其他 SELECT 语句,请确保它们将结果插入到表或变量中。

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 ;

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.如果使用临时表是您唯一的选择,请考虑使用 MEMORY 存储引擎。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM