Mysql存储过程拆分字符串

时间:2016-12-06 16:48:09

标签: sql stored-procedures mysqli split

我写一个存储过程来拆分字符串,并插入其他形式,但结果不是我想要的

感谢您的帮助

示例我想:

在:

aaa§bbb§ccc ...

后:

1:aaa
1:bbb
1:ccc

在这种情况下

在:

aaa§bbb§ccc ...

后:

1:aaa 
1:aaa 
1:aaa ...loop

我知道问题就在这一行,但我不知道写出正确的结果

SET authorAccounts_ = SUBSTRING(authorAccounts_, CHAR_LENGTH(account));

然后我改为这一行它接近我想要的但仍然不是

SET authorAccounts_ = SUBSTRING_INDEX(authorAccounts_, '§', -1);


drop procedure if exists TEST;
delimiter //
CREATE PROCEDURE TEST()
BEGIN
  DECLARE done INT DEFAULT 0;
  DECLARE searchFormId_ bigint;
  DECLARE authorAccounts_ nvarchar(2000);
  DECLARE account nvarchar(200);
  DECLARE c CURSOR FOR
          select searchFormId, authorAccounts
          from test.standardsearchform;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

  OPEN c;

  REPEAT
    FETCH c INTO searchFormId_, authorAccounts_;
    IF NOT done THEN
      WHILE (CHAR_LENGTH(authorAccounts_) > 0) do
         IF (LOCATE('§', authorAccounts_) > 0) then
            SET account = SUBSTRING_INDEX(authorAccounts_,'§',1);
            SET authorAccounts_ = SUBSTRING(authorAccounts_,CHAR_LENGTH(account));
         ELSE
            SET account = authorAccounts_;
            SET authorAccounts_ = NULL;
         END IF;
         INSERT INTO test.standardsearchform_author(searchFormId, authorAccount) VALUES (searchFormId_, account);
      END WHILE;
    END IF;
  UNTIL done END REPEAT;
  CLOSE c;
END//
delimiter ;
show PROCEDURE status;

0 个答案:

没有答案