MySQL 游标使用 多字段

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
原文地址:https://www.cnblogs.com/eason-d/p/10811606.html