credit_bd_sync.sql 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. drop procedure if exists credit_bd_sync;
  2. DELIMITER //
  3. create procedure credit_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_credit cursor for
  16. select crdt.id
  17. from dbr_credit as crdt
  18. where credit_date between _sdate and _edate
  19. order by credit_date asc, crdt.id asc;
  20. Declare continue handler for
  21. not found set _not_found = true;
  22. -- 지우는 범위는 avail_date가 아니다. 전표발생일자 기준
  23. delete from dbr_credit_bd where occur_date between _sdate and _edate;
  24. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  25. open _line_cur_sales;
  26. loop_sales: loop
  27. fetch _line_cur_sales into _line_id;
  28. if _not_found then leave loop_sales; end if;
  29. call credit_bd_act('sordbd', _line_id, 0);
  30. end loop loop_sales;
  31. close _line_cur_sales;
  32. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  33. open _line_cur_credit;
  34. loop_credit: loop
  35. fetch _line_cur_credit into _line_id;
  36. if _not_found then leave loop_credit; end if;
  37. call credit_bd_act('credit', _line_id, 0);
  38. end loop loop_credit;
  39. close _line_cur_credit;
  40. END;
  41. //
  42. DELIMITER ;