<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>

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

        Procedure to loop through comma separated string is not working(遍历逗号分隔字符串的过程不起作用)
        • <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.

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

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

                  相关文档推荐

                  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 - 如何退出/退出存储过程)

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

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

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

                        • <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>