我正在尝试使用从另一个查询接收到的一组值来调用存储过程,并且我想知道如何使用查询中的值调用另一个过程。这是我的代码
CREATE DEFINER=`root`@`localhost` PROCEDURE `temp`(IN u_id int)
BEGIN
#below query will give me all the u_id values that i need to use(ex : 2,8,9)
Declare cur cursor for select r_id from temp.usr_rl where u_id in (u_id);
#below i would like to use the u_id values and run the below procedure in a loop for each value in u_id
open cur;
repeat
fetch cur into a;
if not done then
call get_r(a);
end if;
until done end repeat;
close cur;
END
最佳答案
这会导致巨大的性能损失,因为您使用游标来处理每条记录并调用另一个过程。因此,您实际上使效果加倍。相反,使用如下所示的 CTAS
获取并存储 临时表
中的所有值,并从过程中调用 get_r
访问该临时表以进行任何操作您正在进行的进一步处理。
create temporary table myTemp
as select r_id from temp.usr_rl where u_id = u_id;
关于MySQL For Each 具有特定值的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41186625/