統計存儲過程,這裡是將統計的結果插入一個表中,後臺可以有定時任務來調此存儲過程。以下業務是統計倉庫中商品流轉情況,包括:日期、商品總數、入庫數量、出庫數量。BEGIN DECLARE ES_COR_CODE VARCHAR(20); -- 需要定義接收游標數據的變數 DECLARE...
統計存儲過程,這裡是將統計的結果插入一個表中,後臺可以有定時任務來調此存儲過程。以下業務是統計倉庫中商品流轉情況,包括:日期、商品總數、入庫數量、出庫數量。
BEGIN DECLARE ES_COR_CODE VARCHAR(20); -- 需要定義接收游標數據的變數 DECLARE table_ VARCHAR(50); -- 遍曆數據結束標誌 DECLARE done INT DEFAULT FALSE; -- 游標 DECLARE cur CURSOR FOR SELECT TABLE_NAME FROM (SELECT TABLE_NAME from information_schema.tables WHERE TABLE_NAME LIKE '%wm_item_transfer_%' AND TABLE_SCHEMA='wmscloud_test' ) allTables; -- 將結束標誌綁定到游標 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 打開游標 OPEN cur; -- 開始迴圈 read_loop: LOOP -- 提取游標里的數據,這裡只有一個,多個的話也一樣; FETCH cur INTO table_; -- 聲明結束的時候 IF done THEN LEAVE read_loop; END IF; SET ES_COR_CODE=substring(table_,18); SET @INSERT_ITEM_TRANSFER=CONCAT('INSERT INTO wm_item_transfer_',ES_COR_CODE,' (WMIT_DATE,WMIT_STOCK_NUM,WMIT_INTO_ITEM_NUM,WMIT_OUT_ITEM_NUM) SELECT (SELECT date_sub(curdate(),interval 1 day) ) WMIT_DATE , (SELECT IFNULL(SUM(WMST_NOW_NUMBER),0) FROM wm_stock_',ES_COR_CODE,') WMIT_STOCK_NUM, (SELECT IFNULL(SUM(WMST_NOW_NUMBER),0) FROM wm_stock_',ES_COR_CODE,' WHERE WMST_RECEIVE_DATE BETWEEN date_sub(curdate(),interval 1 day) AND NOW() ) WMIT_INTO_ITEM_NUM, (SELECT IFNULL(SUM(WMOO_OUT_NUMBER),0) FROM wm_out_order_',ES_COR_CODE,' where SURE_DATE BETWEEN date_sub(curdate(),interval 1 day) AND NOW() ) WMIT_OUT_ITEM_NUM'); PREPARE INSERT_ITEM_TRANSFER FROM @INSERT_ITEM_TRANSFER; EXECUTE INSERT_ITEM_TRANSFER; END LOOP; -- 關閉游標 CLOSE cur; END