reward_bd_sync.sql 1.9 KB

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