0

这是我尝试过的。我无法将我的值插入表中。谁能帮我吗 ..

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 ;
4

1 回答 1

0

确实缺少13.6.7.2。声明...处理程序语法

...
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`;
于 2013-10-15T13:42:20.790 回答