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

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

      <tfoot id='jgmL0'></tfoot>

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

        遍历逗号分隔字符串的过程不起作用

        时间:2023-10-26
        • <small id='nMgas'></small><noframes id='nMgas'>

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

                  <bdo id='nMgas'></bdo><ul id='nMgas'></ul>
                    <tbody id='nMgas'></tbody>

                  本文介绍了遍历逗号分隔字符串的过程不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在堆栈溢出中给出的答案的帮助下更正了代码.我想遍历逗号分隔的 Id 字符串,但无法这样做.下面给出的过程只更新第一条记录,不更新其他记录.需要进行哪些更正,以便我可以遍历逗号分隔的字符串.这是我的 SP 的代码

                  I have corrected the code with the help of answer given in stack overflow. I want to loop through the comma separated string of Ids but not able to do so. Below given procedure only updates first record and not updating other records. What corrections are required so that I can loop through comma separated string. This is the code for my SP

                  BEGIN
                    DECLARE strLen    INT DEFAULT 0;
                    DECLARE SubStrLen INT DEFAULT 0;
                  
                    IF strIDs IS NULL THEN
                     SET strIDs = '';
                    END IF;
                  
                  do_this:
                  LOOP
                  SET strLen = LENGTH(strIDs);
                  
                  UPDATE TestTable SET status = 'C' WHERE Id = SUBSTRING_INDEX(strIDs, ',', 1);
                  
                  SET SubStrLen = LENGTH(SUBSTRING_INDEX(strIDs, ',', 1));
                  SET strIDs = MID(strIDs, SubStrLen, strLen);
                  
                  IF strIDs = NULL THEN
                    LEAVE do_this;
                  END IF;
                  END LOOP do_this;
                  END
                  

                  代码在这篇文章的答案中提供.

                  The code is as provided in the answers for this post.

                  我尝试使用 find_in_set() 函数,但它仅在我从头开始传递 id 时才有效,如果我随机传递 Id 则不起作用.这是我的表格脚本

                  I tried with find_in_set() function but it works only if I pass ids start from beginning and not working if I pass Ids randomly. This is my script for the table

                  CREATE TABLE `testtable` (
                  

                  Id int(11) DEFAULT NULL,状态 varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

                  Id int(11) DEFAULT NULL, Status varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

                  -- ----------------------------
                  -- Records of testtable
                  -- ----------------------------
                  INSERT INTO `testtable` VALUES ('1', 'O');
                  INSERT INTO `testtable` VALUES ('2', 'O');
                  INSERT INTO `testtable` VALUES ('3', 'O');
                  INSERT INTO `testtable` VALUES ('4', 'O');
                  INSERT INTO `testtable` VALUES ('5', 'O');
                  

                  这是存储过程开始UPDATE TestTable SET status = 'C' WHERE Id = FIND_IN_SET(Id, strIDs);结束

                  This is the stored procedure BEGIN UPDATE TestTable SET status = 'C' WHERE Id = FIND_IN_SET(Id, strIDs); END

                  strIds 是 varchar 类型.

                  strIds is varchar type.

                  现在试试@strIDs='2'

                  Now try @strIDs='2'

                  推荐答案

                  试试这个(语法错误已修复,没有 CAST 函数)-

                  Try this one (syntax errors are fixed and without CAST function) -

                  DELIMITER $$
                  
                  CREATE PROCEDURE procedure1(IN strIDs VARCHAR(255))
                  BEGIN
                    DECLARE strLen    INT DEFAULT 0;
                    DECLARE SubStrLen INT DEFAULT 0;
                  
                    IF strIDs IS NULL THEN
                      SET strIDs = '';
                    END IF;
                  
                  do_this:
                    LOOP
                      SET strLen = LENGTH(strIDs);
                  
                      UPDATE TestTable SET status = 'C' WHERE Id = SUBSTRING_INDEX(strIDs, ',', 1);
                  
                      SET SubStrLen = LENGTH(SUBSTRING_INDEX(strIDs, ',', 1));
                      SET strIDs = MID(strIDs, SubStrLen, strLen);
                  
                      IF strIDs = NULL THEN
                        LEAVE do_this;
                      END IF;
                    END LOOP do_this;
                  
                  END
                  $$
                  
                  DELIMITER ;
                  

                  您可以使用 MySQL 调试器调试您的程序.

                  You can debug your procedure with Debugger for MySQL.

                  我会在没有程序的情况下做到这一点.尝试使用 FIND_IN_SET 函数,例如-

                  I'd do it without procedure. Try to use FIND_IN_SET function, e.g. -

                  SET @strIDs = '1,2,3'; -- your id values
                  UPDATE TestTable SET status = 'C' WHERE FIND_IN_SET(id, @strIDs);
                  

                  或者创建一个临时文件.表,用 id 值填充它并在 UPDATE 语句中连接这两个表.

                  Or create a temp. table, fill it with id values and join these two tables in UPDATE statement.

                  这篇关于遍历逗号分隔字符串的过程不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为 MySQL 中查询返回的每一行调用一个存储过程 下一篇:是否可以从另一个 sql 脚本中的存储过程调用 sql 脚本?

                  相关文章

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

                  <legend id='E5zt3'><style id='E5zt3'><dir id='E5zt3'><q id='E5zt3'></q></dir></style></legend>

                    • <bdo id='E5zt3'></bdo><ul id='E5zt3'></ul>

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

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