<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 中查询返回的每一行调用一个存储过程

        Call a stored procedure for each row returned by a query in MySQL(为 MySQL 中查询返回的每一行调用一个存储过程)
      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 中查询返回的每一行调用一个存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Accessing another user#39;s table within an Oracle Stored Procedure(在 Oracle 存储过程中访问另一个用户的表)
                  How to View Oracle Stored Procedure using SQLPlus?(如何使用 SQLPlus 查看 Oracle 存储过程?)
                  How to Pass Java List of Objects to Oracle Stored Procedure Using MyBatis?(如何使用 MyBatis 将 Java 对象列表传递给 Oracle 存储过程?)
                  Set the variable result, from query(设置变量结果,来自查询)
                  What is dynamic SQL?(什么是动态 SQL?)
                  Mysql - How to quit/exit from stored procedure(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>

                            <tbody id='AiQkn'></tbody>

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