具有多个值的存储过程问题

时间:2019-05-20 18:02:42

标签: mysql stored-procedures

在存储过程中遇到以下错误。

错误代码:1064 您的SQL语法有误;检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在调用SP时在第1行的'NULL'附近使用。非常感谢您的帮助。

DELIMITER $$

USE `pms_live_0515`$$

DROP PROCEDURE IF EXISTS `split_string1`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `split_string1`()
BEGIN

DECLARE my_delimiter CHAR(1);
DECLARE split_string VARCHAR(255);
DECLARE done INT;
DECLARE occurance INT;
DECLARE i INT;
DECLARE split_id INT;
DECLARE created_at DATETIME;
DECLARE ins_query VARCHAR(500);
DECLARE splitter_cur CURSOR FOR
SELECT id, assigned_to,created_at FROM tasks WHERE id = 1270;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN splitter_cur;
splitter_loop:LOOP
      FETCH splitter_cur INTO split_id,split_string,created_at;

SET occurance=LENGTH(split_string)-LENGTH(REPLACE(split_string,',',''))+1;
SET my_delimiter=',';
  IF done=1 THEN
    LEAVE splitter_loop;
  END IF;
#  select occurance;
  IF occurance > 0 THEN
    #select occurance;
    SET i=1;
    WHILE i <= occurance DO
#        select concat("SUBSTRING_INDEX(SUBSTRING_INDEX( '",split_string ,"', '",my_delimiter,"', ",i, "),'",my_delimiter,"',-1);");
        SET ins_query=CONCAT("insert into my_splits(splitted_column,id,created_at) values(", CONCAT("SUBSTRING_INDEX(SUBSTRING_INDEX( '",split_string ,"', '",my_delimiter,"', ",i, "),'",my_delimiter,"',-1),",split_id,",",created_at,");"));
    SELECT ins_query;
        SET @ins_query=ins_query;
        PREPARE ins_query FROM @ins_query;
        EXECUTE ins_query;
      SET i=i+1;
    END WHILE;
  ELSE
        SET ins_query=CONCAT("insert into my_splits(splitted_column) values(",split_string,"');");
        SET @ins_query=ins_query;
        PREPARE ins_query FROM @ins_query;
        EXECUTE ins_query;
  END IF;
  SET occurance=0;
END LOOP;

CLOSE splitter_cur;

END$$

DELIMITER ;

0 个答案:

没有答案