1. CREATE PROCEDURE weather_day_precipitation (IN DAY INT(10)) COMMENT '一天的降雨量处理'
    2. BEGIN
    3. DECLARE total_precipitation DOUBLE (10, 2) DEFAULT 0.00;
    4. DECLARE precipitation DOUBLE (10, 2) DEFAULT 0.00;
    5. DECLARE weather_id INT DEFAULT 0;
    6. DECLARE done BOOLEAN DEFAULT 0;
    7. -- 当天未处理数据列表
    8. DECLARE precipitation_list CURSOR FOR SELECT
    9. weather_id,
    10. precipitation
    11. FROM
    12. cf_weather_data
    13. WHERE
    14. statice_done = 0
    15. AND TO_DAY (monitor_time) = DAY
    16. ORDER BY
    17. monitor_time ASC;
    18. -- SQLSTATE02000时设置done值为1
    19. DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'
    20. SET done = 1;
    21. -- 当天最后的处理数据
    22. SELECT
    23. add_up_precipitation
    24. FROM
    25. cf_weather_data
    26. WHERE
    27. statice_done = 1
    28. AND TO_DAY (monitor_time) = DAY
    29. ORDER BY
    30. monitor_time DESC
    31. LIMIT 1 INTO total_precipitation;
    32. -- 打开游标
    33. OPEN precipitation_list;
    34. -- 开始循环
    35. REPEAT
    36. -- 把当前行的值赋给声明的局部变量o
    37. FETCH precipitation_list INTO weather_id,
    38. precipitation;
    39. SET total_precipitation = total_precipitation + precipitation;
    40. UPDATE cf_weather_data
    41. SET add_up_precipitation = total_precipitation
    42. WHERE
    43. weather_id = weather_id;
    44. -- done为真时停止循环
    45. UNTIL done
    46. END
    47. REPEAT
    48. ;
    49. -- 关闭游标
    50. CLOSE precipitation_list;
    51. -- 更新当天未处理的全天降雨值
    52. UPDATE cf_weather_data
    53. SET all_day_precipitation = total_precipitation
    54. WHERE
    55. TO_DAY (monitor_time) = DAY;
    56. END;