我有以下存储过程,我需要为类别、功能和状态列插入一组数据。其中类别始终应为“1”,功能应为 1 至 80,状态应始终为“ACTIVE”。
BEGIN
DECLARE x INT;
SET x = 1;
WHILE x <= 80 DO
insert into functiontocategory (category,`function`,`status`) values ('1',x,'ACTIVE');
SET x = x+1;
END WHILE;
END
但是它给了我 160 行插入数据,其中 2 组 1 到 80 秒而不是一组。我的程序出了什么问题。
最佳答案
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `test`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
BEGIN
DECLARE X INT;
SET X = 1;
WHILE X <= 80 DO
INSERT INTO functiontocategory (category,`function`,`status`) VALUES ('1',X,'ACTIVE');
SET X = X+1;
END WHILE;
END$$
DELIMITER ;
我创建了一个数据库作为 test 和一个表 functiontocategory (category int(3),function text,status varchar(10)) 和一个名为 test 的过程..为了测试我使用了 call test() 并插入了 80精确的行
关于Mysql存储过程while循环两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30029706/