为什么游标在过程中取空?

yrwegjxp  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(369)

我正试着做一个程序,从另一个表填充一个表。
但是当我调用这个过程时,它会填充另一个表的空值。
程序如下:

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中,有大约50000条记录循环工作,因为在persons表中,我在调用过程后得到大约50000条记录,但hostname等于null。
你知道吗?

f0ofjuux

f0ofjuux1#

我在这里得到了问题的答案:mysql cursor fetch null
错误是变量名和hostname can属性名´不可能是一样的。
修复程序:

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 ;

相关问题