日期:2014-05-16 浏览次数:20769 次
DELIMITER $$ DROP PROCEDURE IF EXISTS `data_test`.`gps`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `gps`() BEGIN DECLARE i int DEFAULT 1; DECLARE j int DEFAULT 1; declare k datetime default '2011-01-01 00:00:00'; WHILE j<=100 DO while i<=86400 do INSERT INTO T_devdata_gps_20110101(f_devid,f_longitude,f_latitude,f_speed,f_direction,f_gpstime,f_instime) VALUES(CONCAT('f_devid',j),concat('f_longitude',j+0.2),concat('f_latitude',j+0.3),concat('f_speed',j+1),concat('f_direction',j+4),k,k); set k = (SELECT DATE_ADD(k,INTERVAL 1 SECOND)); set i = i+1; end while; set j = j+1; set k = '2011-01-01 00:00:00'; END WHILE; END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `gps`() BEGIN DECLARE i int DEFAULT 1; DECLARE j int DEFAULT 1; declare k datetime default '2011-01-01 00:00:00'; WHILE j<=100 DO SET I=1; # 需要在这里给内层循环的值归位,要么I全局一次后就是86400了,下面就不再循环了。楼主看到的其实是内层循环只做了一次,外层循环是正常的。 while i<=86400 do INSERT INTO T_devdata_gps_20110101(f_devid,f_longitude,f_latitude,f_speed,f_direction,f_gpstime,f_instime) VALUES(CONCAT('f_devid',j),concat('f_longitude',j+0.2),concat('f_latitude',j+0.3),concat('f_speed',j+1),concat('f_direction',j+4),k,k); set k = (SELECT DATE_ADD(k,INTERVAL 1 SECOND)); set i = i+1; end while; set j = j+1; set k = '2011-01-01 00:00:00'; END WHILE; END$$
------解决方案--------------------
DELIMITER $$ DROP PROCEDURE IF EXISTS `data_test`.`gps`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `gps`() BEGIN DECLARE i int DEFAULT 1; DECLARE j int DEFAULT 1; declare k datetime default '2011-01-01 00:00:00'; -- j=1这里也初始化一下,当然这个没有必要,因为在最外层,但是写出来就是告诉楼主, -- 多层循环中,循环变量在循环之前都需要重新置为初始值。 set j=1; WHILE j<=100 DO -- 在每一层开始循环的时候,都需要重新置为初始值,不然i就为86400了,你只执行了j=1这个情况一遍而已。 set i=1; while i<=86400 do INSERT INTO T_devdata_gps_20110101 (f_devid,f_longitude,f_latitude,f_speed,f_direction,f_gpstime,f_instime) VALUES(CONCAT('f_devid',j),concat('f_longitude',j+0.2), concat('f_latitude',j+0.3),concat('f_speed',j+1), concat('f_direction',j+4),k,k); set k = (SELECT DATE_ADD(k,INTERVAL 1 SECOND)); set i = i+1; end while; set j = j+1; set k = '2011-01-01 00:00:00'; END WHILE; END$$