MySql存储过程/函数可以在不使用临时表的情况下返回表吗?
创建以下程序
CREATE PROCEDURE database.getExamples()
SELECT * FROM examples;
然后用
调用它CALL database.getExamples()
显示示例表 - 正如预期的那样 - 但似乎不可能出现以下情况:
SELECT * FROM CALL database.getExamples()
是否有可能从存储过程/函数返回查询结果表,如果是 - 如何?
答案 0 :(得分:18)
至于现在,这是不可能的。
以下是FROM
条款中可能使用的documentation:
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] ...
如您所见,存储过程不在此列表中。
答案 1 :(得分:10)
根据A.4 MySQL 5.6 FAQ: Stored Procedures and Functions:
A.4.14: Can MySQL 5.6 stored routines return result sets?
存储过程可以,但存储的功能不能。
答案 2 :(得分:2)
看起来可以完成,但是使用存储过程中的输出变量。 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
答案 3 :(得分:1)
您可以使用视图而不是存储过程来执行您尝试的操作,但这完全取决于存储过程的作用。
如果使用临时表是您唯一的选择,请考虑使用MEMORY存储引擎。
答案 4 :(得分:1)
每个未插入表或变量的SELECT语句都将生成结果集。
如果希望存储过程只返回一个结果集,请确保只有一个SELECT语句。如果您有其他SELECT语句,请确保它们将结果插入表或变量中。
UPDATE 以下是存储过程的示例。
此存储过程将返回一个结果集:
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 ;