飞道的博客

mysql存储过程实战

509人阅读  评论(0)

今天科比离去,今天肺炎病毒持续肆虐。。。

意识到生命的脆弱,今天我继续前行,比以往更加坚定和紧迫,这辈子不活好自己就算白来一趟。

 最近需要用到mysql存储过程去处理一些表数据,然后利用java poi导出excel数据。

既然用了那就总结一下用到的知识点:

1.声明变量

declare

2.参数

in/out 参数名 数据类型

3.查询赋值

select into   和oracle相似。

4.结果集循环

声明游标,然后打开赋值循环,再赋值。

5.case..when语句

可以直接case

6.条件控制语句

if .. then

else

end if;

7.循环语句

对比oracle for循环,使用while

 

8.mybatis如何调用?

 


  
  1. <select id= "updateWorkDt" parameterType= "map" useCache= "false" statementType= "CALLABLE">
  2. <![CDATA[
  3. call proc_update_work_dt(#{year,mode=IN,jdbcType=VARCHAR},#{month,mode=IN,jdbcType=VARCHAR});
  4. ]]>
  5. </select>

 

 

有不懂得直接看我的存储过程(省略了部分)


  
  1. DROP PROCEDURE IF EXISTS `proc_update_work_dt`;
  2. CREATE PROCEDURE `proc_update_work_dt`( in p_year varchar( 32), in p_month varchar( 32))
  3. BEGIN
  4. declare v_total double DEFAULT 0;
  5. declare v_hours double DEFAULT 0;
  6. declare v_begin_dt VARCHAR( 100);
  7. declare v_end_dt VARCHAR( 100);
  8. declare v_work_day VARCHAR( 100);
  9. declare v_work_time VARCHAR( 100);
  10. declare v_count int; /*总记录数*/
  11. declare i int DEFAULT 1;
  12. declare j int DEFAULT 0;
  13. declare no_more_row int default 0;
  14. declare v_seqno int;
  15. declare v_d1 VARCHAR( 100);
  16. declare v_d2 VARCHAR( 100);
  17. declare v_d3 VARCHAR( 100);
  18. ...
  19. declare c_dt cursor for
  20. select seqno,d1,d2,d3,d4,d5,d6,d7 from work_dt;
  21. declare CONTINUE HANDLER for not found
  22. set no_more_row= 1;
  23. open c_dt;
  24. fetch c_dt into v_seqno,v_d1,v_d2,v_d3,v_d4,v_d5,v_d6,v_d7;
  25. while !no_more_row DO
  26. set v_total = 0;
  27. set i = 1;
  28. while i <= 31 DO
  29. set v_work_day = CONCAT(p_year, '-',p_month, '-',i, ' ');
  30. case i
  31. when 1 then
  32. set v_work_time = v_d1;
  33. when 2 then
  34. set v_work_time = v_d2;
  35. when 3 then
  36. ...
  37. end case;
  38. select substr(v_work_time, 1, instr(v_work_time, '-') -1), substr(v_work_time, instr(v_work_time, '-')+ 1, length(v_work_time))
  39. into v_begin_dt, v_end_dt;
  40. set v_hours = 0;
  41. if(v_begin_dt is not null and v_begin_dt!='' and v_end_dt is not null and v_end_dt != '') then
  42. set v_begin_dt = concat(v_work_day,v_begin_dt);
  43. set v_end_dt = concat(v_work_day,v_end_dt);
  44. select truncate( timestampdiff( MINUTE, DATE_FORMAT(v_begin_dt, '%Y-%m-%d %H:%i'), DATE_FORMAT(v_end_dt, '%Y-%m-%d %H:%i'))/ 60, 1)
  45. into v_hours;
  46. set v_total = v_total+v_hours;
  47. case i
  48. when 1 then
  49. update work_dt set h1 = v_hours where seqno = v_seqno;
  50. when 2 then
  51. update work_dt set h2 = v_hours where seqno = v_seqno;
  52. when 3 then
  53. update work_dt set h3 = v_hours where seqno = v_seqno;
  54. ...
  55. end case;
  56. else
  57. case i
  58. when 1 then
  59. update work_dt set h1 = null where seqno = v_seqno;
  60. when 2 then
  61. update work_dt set h2 = null where seqno = v_seqno;
  62. when 3 then
  63. ...
  64. end case;
  65. end if;
  66. set i=i+ 1;
  67. end WHILE;
  68. update work_dt
  69. set total = v_total
  70. where seqno = v_seqno;
  71. fetch c_dt into v_seqno,v_d1,v_d2,v_d3
  72. end while;
  73. close c_dt;
  74. end;

 

 

 

 


转载:https://blog.csdn.net/IndexMan/article/details/104095784
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场