简体   繁体   English

将SELECT的结果发送到MySQL中的存储过程

[英]Sending results of a SELECT to stored procedure in MySQL

I have a stored procedure which takes one parameter. 我有一个带一个参数的存储过程。 Ordinarily, it will be called as needed on a single id. 通常,将根据需要在单个id上调用它。 Sometimes, though, it would be helpful to run it on every result from a SELECT. 但是,有时候,对SELECT的每个结果运行它会有所帮助。 How can I take the results of a SELECT statement and run the stored procedure on each result? 如何获取SELECT语句的结果并对每个结果运行存储过程?

This isn't directly possible, but... 这不是直接可能的,但是...

As long as the stored procedure doesn't return a result-set, you can write a stored function that accepts the desired input and calls the procedure with the same value as an argument. 只要存储过程不返回结果集,您就可以编写一个存储函数来接受所需的输入,并以与参数相同的值调用该过程。 If a stored function is used in a select query, it will generally be called for each row returned, unless it's referenced in the where clause, in which case, the optimizer may call it for more rows than you might expect, depending on the query plan. 如果在选择查询中使用了存储的函数,则通常会为返回的每一行调用该函数,除非在where子句中引用了这种情况,在这种情况下,优化程序可能会调用比您期望的更多的行,具体取决于查询计划。

Or you can move the logic into a stored function directly instead of using a procedure at all. 或者,您可以直接将逻辑移至存储的函数中,而不必使用任何过程。

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

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