Can a MySql stored procedure \/ function return a table without the use of temp table?
CREATE PROCEDURE database.getExamples()
SELECT * FROM examples;
As for now, this is not possible.
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>
-- 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 have other SELECT statements, make sure that they insert results into a table or variable.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.