mysql 存储过程实例 (日期以小时递增 while loop循环嵌套 随机数生成)
2013-01-17 14:33
701 查看
直接上代码:
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;
相关文章推荐
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- 循环向存储过程中插入100条日期格式化的数据序列号递增
- [轉]MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- Mysql存储过程循环内嵌套使用游标示例
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等(转)
- mysql获取某个范围内的随机数,写了个存储过程自动生成随机6为密码
- Mysql存储过程循环内嵌套使用游标示例代码
- Mysql存储过程循环内嵌套使用游标示例代码
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- 一个MSSql的存储过程---生成编码,形如HEAD20060512000001;(头+日期+6位递增整数)后面为六位,每新的一年编号重新归0
- MySQL存储过程例子,包含事务,参数,嵌套调用,游标,循环等
- 存储过程实例2:通过把group by 放入循环,对生成的主键可以进行其他处理