我尝试了两种方法来重复一些语法正确的代码,但似乎循环(既不是LOOP也不是WHILE)都无法执行。
1使用WHILE:
SET @y =1;
DROP PROCEDURE IF EXISTS parm_y;
DELIMITER //
CREATE PROCEDURE parm_y()
BEGIN
WHILE @y != 62 DO //stop condition
INSERT INTO parm_y SELECT * FROM tempff WHERE op0=@y LIMIT 1;// repeating work
SELECT addr INTO @y FROM tempff WHERE op0=@y LIMIT 1; // repeating work
END WHILE;
END //
DELIMITER ;
2使用LOOP:
SET @y =1;
DROP PROCEDURE IF EXISTS parm_y;
DELIMITER //
CREATE PROCEDURE parm_y()
BEGIN
y_loop: LOOP
INSERT INTO parm_y SELECT * FROM tempff WHERE op0=@y LIMIT 1; // repeating work
SELECT addr INTO @y FROM tempff WHERE op0=@y LIMIT 1; //repeating work
IF @y = 62 THEN //stop condition
LEAVE y_loop;
END IF;
END LOOP;
END //
DELIMITER ;
我不想手动重复这两行。这两段的语法都是正确的。但是没有一个进入循环。
任何人都可以帮助我吗?非常感谢你。
答案 0 :(得分:1)
问题是你没有递增你的计数器@y
。它应该像
SET @y =1;
DROP PROCEDURE IF EXISTS parm_y; DELIMITER // CREATE PROCEDURE parm_y()
BEGIN
WHILE @y < 62 DO //stop condition (changed from `!=` to `<`)
INSERT INTO parm_y SELECT * FROM tempff WHERE op0=@y LIMIT 1;// repeating work
SELECT addr INTO @y FROM tempff WHERE op0=@y LIMIT 1; // repeating work
SET @y = @y + 1; // Missing this part
END WHILE;
END
// DELIMITER ;