stock_io_sync.sql 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. drop procedure if exists stock_io_sync;
  2. DELIMITER //
  3. create procedure stock_io_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 sabd.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_sales_bd as sabd on sobd.id = sabd.sorder_bd_id
  12. inner join dbr_sales as sahd on sahd.id = sabd.sales_id
  13. where sales_date between _sdate and _edate
  14. order by sales_date asc, sabd.id asc;
  15. Declare _line_cur_purch cursor for
  16. select pubd.id
  17. from dbr_porder as pohd
  18. inner join dbr_porder_bd as pobd on pohd.id = pobd.porder_id
  19. inner join dbr_purch_bd as pubd on pobd.id = pubd.porder_bd_id
  20. inner join dbr_purch as puhd on puhd.id = pubd.purch_id
  21. where purch_date between _sdate and _edate
  22. order by purch_date asc, pubd.id asc;
  23. Declare _line_cur_genio cursor for
  24. select gnbd.id
  25. from dbr_genio as gnhd
  26. inner join dbr_genio_bd as gnbd on gnhd.id = gnbd.genio_id
  27. where genio_date between _sdate and _edate
  28. order by genio_date asc, gnbd.id asc;
  29. Declare continue handler for
  30. not found set _not_found = true;
  31. delete from dbr_stock_io where io_date between _sdate and _edate;
  32. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  33. open _line_cur_sales;
  34. loop_sales: loop
  35. fetch _line_cur_sales into _line_id;
  36. if _not_found then leave loop_sales; end if;
  37. call stock_io_act('sales', _line_id, 0);
  38. end loop loop_sales;
  39. close _line_cur_sales;
  40. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  41. open _line_cur_purch;
  42. loop_purch: loop
  43. fetch _line_cur_purch into _line_id;
  44. if _not_found then leave loop_purch; end if;
  45. call stock_io_act('purch', _line_id, 0);
  46. end loop loop_purch;
  47. close _line_cur_purch;
  48. set _not_found = false; -- 이것이 Multi cursor를 쓰는 핵심이다.
  49. open _line_cur_genio;
  50. loop_genio: loop
  51. fetch _line_cur_genio into _line_id;
  52. if _not_found then leave loop_genio; end if;
  53. call stock_io_act('genio', _line_id, 0);
  54. end loop loop_genio;
  55. close _line_cur_genio;
  56. END;
  57. //
  58. DELIMITER ;