zoukankan      html  css  js  c++  java
  • mysql 存储过程

    DELIMITER $$
    
    USE `elephant`$$
    
    DROP PROCEDURE IF EXISTS `procs_cal_order_overdue`$$
    
    CREATE DEFINER=`root`@`%` PROCEDURE `procs_cal_order_overdue`()
    BEGIN
          DECLARE order_code VARCHAR(50);
          DECLARE overdue_status INT;
          DECLARE max_overdue_days INT;
          DECLARE user_id INT;
          DECLARE current_overdue_status INT;
          DECLARE overdue_amount DOUBLE;
          DECLARE current_max_overdue_days INT;
          
          DECLARE done INT DEFAULT FALSE;
          DECLARE i INT DEFAULT 0;
          
          DECLARE cur CURSOR FOR 
          (
            SELECT 
            t1.order_code order_code, 
            CASE WHEN SUM(t1.everoverdue)>0 THEN 1 ELSE 0 END overdue_status,
            MAX(t1.overdays) max_overdue_days,
            t1.user_id user_id,
            CASE WHEN SUM(t1.cover)>0 THEN 1 ELSE 0 END current_overdue_status,
            ROUND(SUM(t1.overamount),2) overdue_amount,
            MAX(t1.coverdays) current_max_overdue_days
            FROM 
            (
            SELECT 
            a.order_code,
            CASE WHEN b.repayment_status='2' THEN d.overdue_days WHEN b.repayment_status='3' THEN c.overdue_days ELSE 0 END overdays,
            CASE WHEN b.repayment_status IN ('2','3') THEN 1 ELSE 0 END everoverdue,
            a.user_id,
            CASE WHEN b.repayment_status='2' THEN 1 ELSE 0 END cover,
            CASE WHEN b.repayment_status IN ('2','5') THEN d.overdue_amount ELSE 0 END overamount,
            CASE WHEN b.repayment_status='2' THEN d.overdue_days ELSE 0 END coverdays
            FROM t_am_bystages_order a
            LEFT JOIN t_am_repayment_plan b ON a.order_id=b.order_id
            LEFT JOIN t_am_repayment_detail c ON b.id=c.repayment_plan_id
            LEFT JOIN t_report_plan_overdue_status d ON b.id=d.plan_id
            WHERE a.status IN ('1','3')
            ) t1
            GROUP BY t1.order_code
          );
          
          DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
          
          SET @exesql = "";      
          SET @exedata = "";      
          
          OPEN cur;
          myloop :LOOP
            FETCH cur INTO order_code,overdue_status,max_overdue_days,user_id,current_overdue_status,overdue_amount,current_max_overdue_days;
            IF i!=0 AND i%1000=0 THEN
               SET @exedata = SUBSTRING(@exedata, 2);
               SET @exesql = CONCAT("REPLACE into t_report_order_overdue_status(order_code,overdue_status,max_overdue_days,user_id,create_time,current_overdue_status,overdue_amount,current_max_overdue_days) values ", @exedata);   
               PREPARE stmt FROM @exesql;
               EXECUTE stmt;
               DEALLOCATE PREPARE stmt;
               SET @exedata = "";
            END IF;        
            IF done THEN 
                 IF i!=0 THEN 
                 SET @exedata = SUBSTRING(@exedata, 2);
                 SET @exesql = CONCAT("REPLACE into t_report_order_overdue_status(order_code,overdue_status,max_overdue_days,user_id,create_time,current_overdue_status,overdue_amount,current_max_overdue_days) values ", @exedata);          
                 PREPARE stmt FROM @exesql;
                 EXECUTE stmt;
                 DEALLOCATE PREPARE stmt;
                 END IF;
               LEAVE myloop;
            END IF;
            SET @exedata = CONCAT(@exedata, ",('",order_code,"',",overdue_status,",",max_overdue_days,",",user_id,",'",NOW(),"',",current_overdue_status,",",overdue_amount,",",current_max_overdue_days,")");
            SET i=i+1;       
            /*REPLACE into t_report_plan_overdue_status (plan_id,overdue_days,overdue_amount,create_time,user_id,order_code,order_id,bs_code) values 
            (planId,overdueDays,overdueAmount,now(),userId,orderCode,orderId,bsCode);*/
          END LOOP;
          CLOSE cur;      
          
        END$$
    
    DELIMITER ;


    主要就是从多张表汇总数据到一张表,涉及到了sql拼接,游标遍历,分批提交

  • 相关阅读:
    HDU5269 字典树
    HDU1664 BFS + 数论 + 剪枝
    HDU1429 BFS + 状态压缩
    HDU1075 字典树 + 字符串映射
    HDU1247 字典树
    UVa 10256(凸包、线段交、点在多边形内)
    UVa 10652(旋转、凸包、多边形面积)
    牛客练习赛43D(贪心)
    牛客练习赛43F(推式子)
    Codeforces 1161B(判断旋转对称)
  • 原文地址:https://www.cnblogs.com/younldeace/p/6934541.html
Copyright © 2011-2022 走看看