Procedure to loop through comma separated string is not working

后端 未结 3 514
抹茶落季
抹茶落季 2020-12-10 06:35

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 proced

3条回答
  •  心在旅途
    2020-12-10 06:56

    I didn't find any suitable method, so I ended up making my own. It's pretty simple.

    PROCEDURE db.loop_through_array()
    BEGIN
    
      DECLARE strIDs varchar(150) DEFAULT 'one,two,three';
      DECLARE element varchar(150);
    
      WHILE strIDs != '' DO
    
        SET element = SUBSTRING_INDEX(strIDs, ',', 1);      
    
        UPDATE TestTable SET status = 'C' WHERE Id = element;
    
        IF LOCATE(',', strIDs) > 0 THEN
          SET strIDs = SUBSTRING(strIDs, LOCATE(',', strIDs) + 1);
        ELSE
          SET strIDs = '';
        END IF;
    
      END WHILE;
    
    END
    

提交回复
热议问题