CREATE DEFINER=`root`@`localhost` FUNCTION `FUN_FIX_RECEIPT_CONTENT_PAYMENT`(accountStartDay varchar(10), accountEndDay varchar(10)) RETURNS int(11) BEGIN #解析原始小票前 刪除解析小票 #shopsId 商家ID #accountDay 10位日期 2018-01-09 -- 定義一個或者多個 變量來接收 遊標查詢的列值 DECLARE receiptContentId INT; DECLARE payFlag VARCHAR(30); DECLARE cashAmount DOUBLE; DECLARE cardAmount DOUBLE; DECLARE scoreAmount DOUBLE; DECLARE thirdAmount DOUBLE; DECLARE alpayAmount DOUBLE; DECLARE wechatAmount DOUBLE; DECLARE otherAmount DOUBLE; -- 遍歷數據結束標誌 DECLARE done INT DEFAULT FALSE; -- 遊標內容 DECLARE cursor_receipt_content CURSOR FOR select id, pay_flag, cash_price, card_price, score_price, third_party_price, alipay_price, wechat_price, other_price from t_shopping_receipt_content where grasping_time >= STR_TO_DATE(CONCAT(accountStartDay,' 00:00:00'), '%Y-%m-%d %H:%i:%s') and grasping_time <= STR_TO_DATE(CONCAT(accountEndDay,' 23:59:59'), '%Y-%m-%d %H:%i:%s'); -- 將結束標誌綁定到遊標 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- 打開遊標 OPEN cursor_receipt_content; r_loop: LOOP -- 提取遊標裏的數據,這裏只有一個,多個用","逗號隔開 FETCH cursor_receipt_content INTO receiptContentId, payFlag, cashAmount, cardAmount, scoreAmount, thirdAmount, alpayAmount, wechatAmount, otherAmount; IF done THEN LEAVE r_loop; END IF; -- 這裏作你想作的循環的事件 if locate('現金',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 1 , cashAmount); end if; if locate('刷卡',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 2 , cardAmount); end if; if locate('積分',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 3 , scoreAmount); end if; if locate('第三方',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 4 , thirdAmount); end if; if locate('支付寶',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 5 , alpayAmount); end if; if locate('微信',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 6 , wechatAmount); end if; if locate('其餘',payFlag) > 0 then INSERT INTO t_receipt_content_payment(receipt_content_id, payment_category_id, amount) VALUES(receiptContentId, 7 , otherAmount); end if; END LOOP r_loop; -- 關閉遊標 CLOSE cursor_receipt_content; RETURN 0; END