MySQL For具有特定值的每个条件

时间:2016-12-16 14:33:42

标签: mysql stored-procedures cursor

我试图使用一组带有我从另一个查询接收的值的存储过程调用我想知道如何使用来自a的值调用另一个过程查询。这是我的代码

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 

1 个答案:

答案 0 :(得分:0)

由于您使用游标处理每条记录并调用另一个过程,因此会导致性能大幅下降。因此,你实际上是效果加倍。相反,使用下面的Temporary Table获取并存储CTAS中的所有值,并从您的过程call get_r中访问该临时表,以便进行进一步的处理。

create temporary table myTemp 
as select r_id from temp.usr_rl where u_id = u_id;