我正在编写一个循环通过各种昆虫收集器的程序,其中一些用逗号分隔,例如。 "诉Shirey,L。Smith"我需要为每个收集器创建一个新行。我的程序如下:
DROP PROCEDURE IF EXISTS agent_reclamation;
DELIMITER //
CREATE PROCEDURE agent_reclamation (IN VerbatimName VARCHAR(170))
BEGIN
DECLARE verbatimNameHandler varchar(170);
DECLARE tempAgentName varchar(170);
SET verbatimNameHandler = VerbatimName;
WHILE LENGTH(verbatimNameHandler) > 0 DO -- while there's more stuff left
IF LOCATE(',', verbatimNameHandler) > 0 THEN -- and theres a comma to be found
SET tempAgentName = SUBSTRING(verbatimNameHandler,1,LOCATE(',',verbatimNameHandler) - 1); -- set the temp variable to everything from the first character to the first comma
ELSE
SET tempAgentName = verbatimNameHandler; -- set the name if there are no commas
SET verbatimNameHandler = ''; -- won't accept procedure without update --
END IF;
INSERT INTO agentReclamation SET tempAgentName = tempAgentName;
/* INSERT INTO agentReclamation(tempAgentName) VALUES (tempAgentName); */ -- insert the new names into the agentReclamation table
SET verbatimNameHandler = REPLACE(verbatimNameHandler, tempAgentName + ',', ''); -- won't accept procedure without update --
END WHILE;
END //
DELIMITER ;
SELECT agent_reclamation(VerbatimName) FROM tempAgent WHERE VerbatimName LIKE
'%,%';
我收到错误1442说明,无法更新表格' tempAgent'在存储函数/触发器中,因为它在调用此存储函数/触发器的语句中使用。我无法确定自己的位置,以便更新“临时代理”。随着程序的任何地方。
任何帮助都将不胜感激,我现在正忙着编写自己的程序。谢谢!
答案 0 :(得分:0)
我所做的是创建两个过程,其中一个过程通过对原始过程的过程调用遍历每一行。见下文:
DROP PROCEDURE IF EXISTS agent_reclamation;
DROP PROCEDURE IF EXISTS procIteration;
DELIMITER //
CREATE PROCEDURE procIteration ()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE verbatimNameHandler varchar(170);
DECLARE cur CURSOR FOR SELECT VerbatimName FROM tempAgent WHERE VerbatimName LIKE '%,%';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
OPEN cur;
testLoop: LOOP
FETCH cur INTO verbatimNameHandler;
IF done THEN
LEAVE testLoop;
END IF;
CALL agent_reclamation(verbatimNameHandler);
END LOOP testLoop;
CLOSE cur;
END //
CREATE PROCEDURE agent_reclamation (IN VerbatimName VARCHAR(170))
BEGIN
DECLARE verbatimNameHandler varchar(170);
DECLARE tempAgentName varchar(170);
SET verbatimNameHandler = VerbatimName;
WHILE LENGTH(verbatimNameHandler) > 0 DO -- while there's more stuff left
IF LOCATE(',', verbatimNameHandler) > 0 THEN -- and theres a comma to be found
SET tempAgentName = SUBSTRING(verbatimNameHandler,1,LOCATE(',',verbatimNameHandler) - 1); -- set the temp variable to everything from the first character to the first comma
ELSE
SET tempAgentName = verbatimNameHandler; -- set the name if there are no commas
SET verbatimNameHandler = ''; -- won't accept procedure without update --
END IF;
-- INSERT INTO agentReclamation SET tempAgentName = tempAgentName;
INSERT INTO agentReclamation(tempAgentName) VALUES (tempAgentName); -- insert the new names into the agentReclamation table
SET verbatimNameHandler = REPLACE(verbatimNameHandler, CONCAT(tempAgentName, ','), ''); -- won't accept procedure without update --
END WHILE;
END //
DELIMITER ;
CALL procIteration();
这会产生预期的结果。