我尝试执行一个过程以从另一个表填充一个表。
但是在调用该过程时,此表将在另一个表中填充为空。
过程如下:
DELIMITER //
CREATE PROCEDURE invent()
BEGIN
DECLARE bDone INT;
DECLARE hostname VARCHAR(100);
DECLARE curs CURSOR FOR
select hostname from GetInvent;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
DROP TABLE Persons;
CREATE TABLE Persons (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hostname` VARCHAR(100),
PRIMARY KEY (`id`)
);
SET bDone = 0;
OPEN curs;
igmLoop: loop
FETCH curs INTO hostname;
if bDone = 1 then leave igmLoop; end if;
INSERT INTO Persons(hostname) VALUES (hostname);
END LOOP igmLoop;
CLOSE curs;
SELECT * FROM Persons;
END //
DELIMITER ;
在GetInvent中大约有50.000条记录了循环工作,因为在Persons表中,调用该过程后,主机名等于Null,我得到了大约50.000条
有什么主意吗?
答案 0 :(得分:0)
我在这里找到了问题的答案:MySQL cursor fetch NULL
错误是变量名,属性主机名的名称不能相同。
修复过程:
DELIMITER //
CREATE PROCEDURE invent()
BEGIN
DECLARE bDone INT;
DECLARE v_hostname VARCHAR(100);
DECLARE curs CURSOR FOR
select hostname from GetInvent;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET bDone = 1;
DROP TABLE Persons;
CREATE TABLE Persons (
`id` int(11) NOT NULL AUTO_INCREMENT,
`hostname` VARCHAR(100),
PRIMARY KEY (`id`)
);
SET bDone = 0;
OPEN curs;
igmLoop: loop
FETCH curs INTO v_hostname;
if bDone = 1 then leave igmLoop; end if;
select v_hostname;
INSERT INTO Persons(hostname) VALUES (v_hostname);
END LOOP igmLoop;
CLOSE curs;
SELECT * FROM Persons;
END //
DELIMITER ;