reward_bd_sync.sql 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. drop procedure if exists reward_bd_sync;
  2. DELIMITER //
  3. create procedure reward_bd_sync( _sdate varchar(8), _edate varchar(8) )
  4. SQL SECURITY INVOKER
  5. BEGIN
  6. Declare _line_id int;
  7. Declare _not_found boolean;
  8. Declare _line_cur_sales cursor for
  9. select sobd.id
  10. from dbr_sorder as sohd
  11. inner join dbr_sorder_bd as sobd on sohd.id = sobd.sorder_id
  12. inner join dbr_item as itm on itm.id = sobd.item_id
  13. where sorder_date between _sdate and _edate
  14. order by sorder_date asc, sobd.id asc;
  15. Declare _line_cur_reward cursor for
  16. select rewd.id
  17. from dbr_reward as rewd
  18. where reward_date between _sdate and _edate
  19. order by reward_date asc, rewd.id asc;
  20. Declare _line_cur_sorder cursor for
  21. select sohd.id
  22. from dbr_sorder as sohd
  23. where sorder_date between _sdate and _edate
  24. order by sorder_date asc, sohd.id asc;
  25. Declare continue handler for
  26. not found set _not_found = true;
  27. -- 지우는 범위는 avail_date가 아니다. 전표발생일자 기준
  28. delete from dbr_reward_bd where occur_date between _sdate and _edate;
  29. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  30. open _line_cur_sales;
  31. loop_sales: loop
  32. fetch _line_cur_sales into _line_id;
  33. if _not_found then leave loop_sales; end if;
  34. call reward_bd_act('sordbd', _line_id, 0);
  35. end loop loop_sales;
  36. close _line_cur_sales;
  37. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  38. open _line_cur_reward;
  39. loop_reward: loop
  40. fetch _line_cur_reward into _line_id;
  41. if _not_found then leave loop_reward; end if;
  42. call reward_bd_act('reward', _line_id, 0);
  43. end loop loop_reward;
  44. close _line_cur_reward;
  45. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  46. open _line_cur_sorder;
  47. loop_sorder: loop
  48. fetch _line_cur_sorder into _line_id;
  49. if _not_found then leave loop_sorder; end if;
  50. call reward_bd_act('sorder', _line_id, 0);
  51. end loop loop_sorder;
  52. close _line_cur_sorder;
  53. END;
  54. //
  55. DELIMITER ;