简体   繁体   English

在另一个存储过程中使用mysql存储过程的结果集

[英]use result set of mysql stored procedure in another stored procedure

I have a MYSQL stored procedure SP1() that returns a result set. 我有一个MYSQL存储过程SP1(),它返回一个结果集。

I want to call SP1() inside of SP2() and loop through the result set of SP1() to do some additional work. 我想在SP2()内部调用SP1()并遍历SP1()的结果集以执行一些其他工作。

I don't want to include my logic from SP1() because it would make SP2() too complicated. 我不想从SP1()中包含我的逻辑,因为这会使SP2()过于复杂。

Any suggestions? 有什么建议么?

Thanks. 谢谢。

What you want to do doesnt sound particularly good and maybe you should think about re-designing those 2 procs. 您想做的事情听起来并不是特别好,也许您应该考虑重新设计这两个过程。 However, you could do something like this as a quick fix: 但是,您可以执行以下操作来快速解决此问题:

get your sp2 sproc to write it's intermediate results to a temporary table which you can then access/process inside of sp1. 使您的sp2 sproc将其中间结果写入临时表,然后可以在sp1内部进行访问/处理。 You can then drop the temporary table which you created in sp2 once sp1 returns. 一旦sp1返回,就可以删除在sp2中创建的临时表。

http://pastie.org/883881 http://pastie.org/883881

delimiter ;
drop procedure if exists foo;
delimiter #

create procedure foo()
begin

  create temporary table tmp_users select * from users;

  -- do stuff with tmp_users

  call bar();

  drop temporary table if exists tmp_users;

end #

delimiter ;

drop procedure if exists bar;

delimiter #

create procedure bar()
begin
  -- do more stuff with tmp_users
  select * from tmp_users;
end #

delimiter ;

call foo();

not very elegant but should do the trick 不是很优雅,但应该可以解决问题

Cursors would help solve the issue. 游标将帮助解决该问题。

I am not sure if this is possible but make a cursor for select call for SP1() and iterate over them as usual cursor. 我不确定这是否可行,但是可以为SP1()的select调用创建一个游标,然后像往常一样游标遍历它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM