直接上代碼:
DELIMITER $$
drop procedure if exists proc_test$$
create procedure proc_test()
begin
declare id int; --對象id
declare done int; --循環結束的標誌
declare in_dateTime date; --循環遞增的起始時間
declare tempVal int; --隨機數
declare cursor_test cursor for select cfldID from cfield; --建立遊標,獲取所有的cfield的id
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1; --設置循環結束的標誌
open cursor_test; --打開遊標
cursor_loop:loop
fetch cursor_test into id;
if done =1 then
leave cursor_loop;
end if;
--設置起始時間值
set in_dateTime = '2012-11-30 00:00:00';
set @in_dateTime = in_dateTime;
--while遞增循環,每次增加一個小時
loop_while: while @in_dateTime < '2012-11-30 23:00:00' do
--生成隨機數
SELECT FLOOR(18 + (RAND() * 7)) into tempVal;
set @tempVal = tempVal;
insert into cdacq(cfldID,HTime,MTime,LTime,FanState,FanPowerState,SupplyTemp,RoomTemp,AcqTime,Alt) values(id,10000,10000,10000,'84','0',42,@tempVal,@in_dateTime,'');
set @in_dateTime = date_add(@in_dateTime, interval '01:00:00' hour_second); --時間增加一小時
end while loop_while; --結束while循環
end loop cursor_loop; --結束loop循環
close cursor_test; --關閉遊標
end $$ --存儲過程結束
DELIMITER;