mysql中的存储过程不会将获取和迭代的值插入表中

时间:2013-10-15 06:32:39

标签: mysql stored-procedures

这是我试过的。我无法将我的值插入表中。任何人都可以帮助我..

DELIMITER $$
USE `SampleDB`$$

DROP PROCEDURE IF EXISTS `Sample`$$

CREATE PROCEDURE `SampleDB`.`Sample`()
BEGIN
    #declare variable
    DECLARE tenantName VARCHAR(255);
    DECLARE tenantAddress VARCHAR(255);
    DECLARE done INT DEFAULT FALSE;
    DECLARE cur1 CURSOR FOR SELECT tenant_name,tenant_address FROM tenant;

    #open cursor
    OPEN cur1;
        #starts the loop
        the_loop: LOOP
            #get the values of each column into our variables
            FETCH cur1 INTO tenantName,tenantAddress;
            IF done THEN
            LEAVE the_loop;
            END IF;     
            #Insert it
            INSERT INTO tenant(tenant_name,tenant_address)
            VALUES (tenantName,tenantAddress);  
        END LOOP the_loop;

    CLOSE cur1;
END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

确实缺少13.6.7.2. DECLARE ... HANDLER Syntax

...
DECLARE cur1 CURSOR FOR SELECT tenant_name, tenant_address FROM tenant;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;

#open cursor
...

但你真的需要光标吗?使用这样的语句,您可以避开光标:

INSERT INTO `tenant` (`tenant_name`, `tenant_address`)
SELECT `tenant_name`, `tenant_address` FROM `tenant`;