stock_io_sync.sql 2.1 KB

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