自己写的一个mysql存储过程

DROP PROCEDURE IF EXISTS `p_sendGold`; 
CREATE PROCEDURE p_sendGold()  
BEGIN  DECLARE done INT DEFAULT 0;
   DECLARE id INT;
   DECLARE num INT;
   DECLARE flag INT;
	 DECLARE accessory1 INT;
   DECLARE cur1 CURSOR FOR SELECT u.user_id,sum(p.num) as sum_num FROM user_info u,pay_his p where u.user_id=p.user_id and date(sub_time) = date_sub(curdate(),interval 1 day) GROUP BY p.user_id ORDER BY sum_num DESC;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;  
   OPEN cur1;
		
	 set flag = 0;

   REPEAT      
      FETCH cur1 INTO id,num;  
      
     
      if(num>=1000&&num<2000) then
      	 set accessory1 = 7081;
      
      ELSEIF(num>=2000&&num<5000) then
      	 set accessory1 = 7082;				
      
      ELSEIF(num>=5000&&num<10000) then
      	 set accessory1 = 7083;				 
      
      ELSEIF(num>=10000&&num<20000) then
      	 set accessory1 = 7084;				
      
      ELSEIF(num>=20000&&num<50000) then
      	 set accessory1 = 7085;				
      
      ELSEIF(num>=50000&&num<100000) then
      	 set accessory1 = 7086;				
      
      ELSEIF(num>=100000) then
      	 set accessory1 = 7087; 
			ELSE
				set accessory1 = 0;
			end IF;
			
			IF(accessory1>0) then
				insert into email_info(send_user_id,get_user_id,title,content,mail_type,send_time,readed,accessory1,accessory2,accessory3,accessory4,accessory1_num,accessory2_num,accessory3_num,accessory4_num,order_code,is_del) values(0,id,'系统邮件','充值奖励',0,now(),0,accessory1,0,0,0,1,0,0,0,0,0);     
			end IF;
		#set flag =flag+1;#记录循环次数
   UNTIL done=1 END REPEAT;
		
	 
	 #SELECT flag,num,id,accessory1;

   CLOSE cur1;
#不知道为什么上面循环最好一条记录老是插入两条记录,所以这里删除一条。
	SELECT max(email_id) into id from email_info where content='充值奖励';
	delete from email_info where email_id=id ;
	
  END

java技术交流群57388149

posted @ 2013-04-10 16:22  流星焱雨  阅读(205)  评论(0编辑  收藏  举报