<legend id='Hq57C'><style id='Hq57C'><dir id='Hq57C'><q id='Hq57C'></q></dir></style></legend>
    1. <small id='Hq57C'></small><noframes id='Hq57C'>

      <tfoot id='Hq57C'></tfoot>

      <i id='Hq57C'><tr id='Hq57C'><dt id='Hq57C'><q id='Hq57C'><span id='Hq57C'><b id='Hq57C'><form id='Hq57C'><ins id='Hq57C'></ins><ul id='Hq57C'></ul><sub id='Hq57C'></sub></form><legend id='Hq57C'></legend><bdo id='Hq57C'><pre id='Hq57C'><center id='Hq57C'></center></pre></bdo></b><th id='Hq57C'></th></span></q></dt></tr></i><div id='Hq57C'><tfoot id='Hq57C'></tfoot><dl id='Hq57C'><fieldset id='Hq57C'></fieldset></dl></div>

          <bdo id='Hq57C'></bdo><ul id='Hq57C'></ul>
      1. 为 MySQL 中查询返回的每一行调用一个存储过程

        时间:2023-10-26
      2. <i id='imkAU'><tr id='imkAU'><dt id='imkAU'><q id='imkAU'><span id='imkAU'><b id='imkAU'><form id='imkAU'><ins id='imkAU'></ins><ul id='imkAU'></ul><sub id='imkAU'></sub></form><legend id='imkAU'></legend><bdo id='imkAU'><pre id='imkAU'><center id='imkAU'></center></pre></bdo></b><th id='imkAU'></th></span></q></dt></tr></i><div id='imkAU'><tfoot id='imkAU'></tfoot><dl id='imkAU'><fieldset id='imkAU'></fieldset></dl></div>
        <legend id='imkAU'><style id='imkAU'><dir id='imkAU'><q id='imkAU'></q></dir></style></legend>
          <bdo id='imkAU'></bdo><ul id='imkAU'></ul>

            <small id='imkAU'></small><noframes id='imkAU'>

              1. <tfoot id='imkAU'></tfoot>

                    <tbody id='imkAU'></tbody>

                • 本文介绍了为 MySQL 中查询返回的每一行调用一个存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想要一个有效的 MySQL 存储过程:

                  I want a MySQL stored procedure which effectively does:

                  foreach id in (SELECT id FROM objects WHERE ... ) CALL testProc(id)

                  我想我只是想要 MySQL 回答这个问题,但我不太了解游标:如何为查询返回的每一行执行一次存储过程?

                  I think I simply want the MySQL answer to this question but I don't understand cursors well: How do I execute a stored procedure once for each row returned by query?

                  推荐答案

                  诸如循环"(for-each、while 等)和分支"(if-else、call 等)等概念是过程性的 并且不存在于像 SQL 这样的声明性语言中.通常可以用声明的方式表达自己想要的结果,这才是解决这个问题的正确方法.

                  Concepts such as "loops" (for-each, while, etc) and "branching" (if-else, call, etc) are procedural and do not exist in declarative languages like SQL. Usually one can express one’s desired result in a declarative way, which would be the correct way to solve this problem.

                  例如,如果要调用的 testProc 过程使用给定的 id 作为另一个表的查找键,那么您可以(并且应该)简单地JOIN 将你的表放在一起——例如:

                  For example, if the testProc procedure that is to be called uses the given id as a lookup key into another table, then you could (and should) instead simply JOIN your tables together—for example:

                  SELECT ...
                  FROM   objects JOIN other USING (id)
                  WHERE  ...
                  

                  只有在您的问题无法以声明方式表达的极少数情况下,您才应该求助于程序性解决问题.存储过程是在 MySQL 中执行过程代码的唯一方法.因此,您要么需要修改现有的 sproc,使其在循环中执行当前的逻辑,要么创建一个新的 sproc,从循环中调用现有的 sproc:

                  Only in the extremely rare situations where your problem cannot be expressed declaratively should you then resort to solving it procedurally instead. Stored procedures are the only way to execute procedural code in MySQL. So you either need to modify your existing sproc so that it performs its current logic within a loop, or else create a new sproc that calls your existing one from within a loop:

                  CREATE PROCEDURE foo() BEGIN
                    DECLARE done BOOLEAN DEFAULT FALSE;
                    DECLARE _id BIGINT UNSIGNED;
                    DECLARE cur CURSOR FOR SELECT id FROM objects WHERE ...;
                    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
                  
                    OPEN cur;
                  
                    testLoop: LOOP
                      FETCH cur INTO _id;
                      IF done THEN
                        LEAVE testLoop;
                      END IF;
                      CALL testProc(_id);
                    END LOOP testLoop;
                  
                    CLOSE cur;
                  END
                  

                  这篇关于为 MySQL 中查询返回的每一行调用一个存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Laravel 4 迁移中创建 MYSQL 过程 下一篇:遍历逗号分隔字符串的过程不起作用

                  相关文章

                  • <bdo id='AiQkn'></bdo><ul id='AiQkn'></ul>
                  <legend id='AiQkn'><style id='AiQkn'><dir id='AiQkn'><q id='AiQkn'></q></dir></style></legend>

                    <tfoot id='AiQkn'></tfoot>
                    <i id='AiQkn'><tr id='AiQkn'><dt id='AiQkn'><q id='AiQkn'><span id='AiQkn'><b id='AiQkn'><form id='AiQkn'><ins id='AiQkn'></ins><ul id='AiQkn'></ul><sub id='AiQkn'></sub></form><legend id='AiQkn'></legend><bdo id='AiQkn'><pre id='AiQkn'><center id='AiQkn'></center></pre></bdo></b><th id='AiQkn'></th></span></q></dt></tr></i><div id='AiQkn'><tfoot id='AiQkn'></tfoot><dl id='AiQkn'><fieldset id='AiQkn'></fieldset></dl></div>

                    <small id='AiQkn'></small><noframes id='AiQkn'>