我想返回一个在存储过程中动态创建的集合(为了简单起见,我们假设集合是在循环内计算的偶数列表。)
我使用过的某些数据库有一个SUSPEND
操作,它将OUT
参数的当前值添加到过程的结果集中,如下所示:
DELIMITER $
CREATE PROCEDURE EvenNumbers(
IN n INT, OUT NUM INT )
BEGIN
DECLARE i INT DEFAULT 0;
WHILE i < n DO
SET NUM = i;
SUSPEND; /* adds a row with a value 'i' for column NUM to the result set */
SET i = i+2;
END WHILE;
END$
DELIMITER ;
SUSPEND
是MySQL中的关键字,但我认为它不会做同样的事情,因为它不起作用(Error: Unexpected 'SUSPEND'
)。
那么,如何从MySQL中的存储过程逐个返回一组行?
答案 0 :(得分:1)
我知道你想要一个偶数发生器? 这是纯MySQL而不使用存储过程。
首先,您需要使用下面的查询生成大量关闭数字。 此查询生成1到10000条记录。
<强>查询强>
SELECT
@row := @row + 1 AS ROW
FROM
(
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row1
CROSS JOIN (
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row2
CROSS JOIN (
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row3
CROSS JOIN (
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row4
CROSS JOIN (
SELECT @row := 0
) init_user_param
主要查询。
<强>查询强>
SELECT
*
FROM (
SELECT
@row := @row + 1 AS ROW
FROM
(
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row1
CROSS JOIN (
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row2
CROSS JOIN (
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row3
CROSS JOIN (
SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9
) row4
CROSS JOIN (
SELECT @row := 0
) init_user_param
)
ROWS
WHERE
ROWS.ROW BETWEEN 1 AND 10
AND
ROWS.ROW MOD 2 = 0
<强>结果强>
row
--------
2
4
6
8
10
答案 1 :(得分:1)
亚历克斯, 您可以在存储过程中创建一个临时表(这是完全正常的),将计算结果存储在其中,并在完成计算后返回临时表的内容。因此,使用过程返回的正常结果集,而不是OUT参数。
答案 2 :(得分:0)
我只是生成JSON并将其作为单个结果返回
Bellow是一个示例查询,尽管很大,但它从联结中清晰可见的不同表中提取内容,但仍返回JSON对象
select
JSON_OBJECT(
'application_id', yvs_job_search_application.application_id,
'progress_id', yvs_job_search_application.progress_id,
'progress_name', yvs_job_search_application_progress.progress_name,
'type_id', yvs_job_search_application.type_id,
'type_name', yvs_job_search_application_type.type_name,
'wage', yvs_job_search_application.wage,
'wage_id', yvs_job_search_application.wage_id,
'wage_name', yvs_job_search_wage_type.wage_name,
'position_name', yvs_job_search_application.position_name,
'agency_refference', yvs_job_search_application.agency_refference,
'position_location', yvs_job_search_application.position_location,
'job_description', yvs_job_search_application.job_description,
'agency_id', yvs_job_search_application.agency_id,
'agency_name', yvs_job_search_agency.agency_name ,
'default_number', yvs_job_search_agency.default_number,
'agency_site', yvs_job_search_agency.agency_site ,
'agent_id', yvs_job_search_application.agent_id,
'agent_name', yvs_job_search_agent.agent_name ,
'agent_email', yvs_job_search_agent.agent_email,
'agent_phone', yvs_job_search_agent.agent_phone ,
'agent_mobile', yvs_job_search_agent.agent_mobile )
from yvs_job_search_application
join yvs_job_search_application_progress on yvs_job_search_application.progress_id = yvs_job_search_application_progress.progress_id
join yvs_job_search_application_type onyvs_job_search_application.type_id = yvs_job_search_application_type.type_id
join yvs_job_search_wage_type on yvs_job_search_application.wage_id = yvs_job_search_wage_type.wage_id
join yvs_job_search_agency on yvs_job_search_application.agency_id = yvs_job_search_agency.agency_id
join yvs_job_search_agent on yvs_job_search_application.agent_id = yvs_job_search_agent.agent_id
;
您可以在mysql上阅读JSON函数 JSON Functions in MySQL