我正在尝试使用 ',' 等分隔符来拆分 eventIDs 的输入。
在此存储过程中的输入示例为('852', '159,11031', '5')。
所以我想调用存储过程sp_case_attach_event两次。
一次使用 ('852', '159', '5') 一次使用 ('852', '11031', '5')
以下代码是由其他人编写的,但无法联系到。但代码不起作用,因为它抛出一个错误:
SQL Fehler (1292): Truncated incorrect INTEGER value:','
我不知道问题出在哪里。
[...]
PROCEDURE `sp_case_attach_event_list`(IN `caseID` INT, IN `eventIDs` text, IN `userID` INT)
[...]
BEGIN
DECLARE inipos INTEGER;
DECLARE endpos INTEGER;
DECLARE maxlen INTEGER;
DECLARE item VARCHAR(100);
DECLARE delim VARCHAR(1);
SET delim = ',';
SET inipos = 1;
SET eventIDs = CONCAT(eventIDs, delim);
SET maxlen = LENGTH(eventIDs);
REPEAT
SET endpos = LOCATE(delim, eventIDs, inipos);
SET item = SUBSTR(eventIDs, inipos, endpos - inipos);
IF item <> '' AND item IS NOT NULL THEN
call sp_case_attach_event(caseID,eventIDs,userID);
END IF;
SET inipos = endpos + 1;
UNTIL inipos >= maxlen END REPEAT;
END
第二个存储过程的代码。它确实适用于示例数据 ('852', '159', '5') 和 ('852', '11031', '5'):
sp_case_attach_event
[..]
PROCEDURE `sp_case_attach_event`(IN `caseID` INT, IN `eventID` INT, IN `userID` INT)
[..]
BEGIN
DECLARE modificationID INT;
select count(ontbl_analyse_event.modification_ID) into @countENTRY from ontbl_analyse_event where ontbl_analyse_event.analyse_ID = caseID and ontbl_analyse_event.event_ID = eventID;
if (@countENTRY = 0) then
insert into ontbl_modification (ontbl_modification.creator_ID,ontbl_modification.date) values (userID,now());
SET modificationID = LAST_INSERT_ID();
insert into ontbl_analyse_modification (ontbl_analyse_modification.analyse_ID,ontbl_analyse_modification.modification_ID,ontbl_analyse_modification.textlog_description) values (caseID,modificationID,'event attached');
insert into ontbl_analyse_event (ontbl_analyse_event.analyse_ID, ontbl_analyse_event.event_ID, ontbl_analyse_event.modification_ID) values (caseID, eventID, modificationID);
end if;
END
最佳答案
我在这里发现一个小问题:
call sp_case_attach_event(caseID,eventIDs,userID);
您不是在循环内修改 eventIDs
变量,而是修改 item
变量。所以我想应该是:
call sp_case_attach_event(caseID,item,userID);
关于mysql - 在存储过程中按分隔符分割文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872801/