mysql数据库存储过程

最近公司有了一个需求,做一个统计表,需要连接很多种表的那种SQL;

想了一下如果在程序里处理这些逻辑,第一程序显得非常臃肿,第二我感觉效率上还是存储过程高点(只不过可移植性很差吧,但是我们公司基本上不会换数据库,所以放心使用);

因为统计合同的服务费,而且需要一次性需要分析3行记录(不合理的数据库结构导致的),因此想采用存储过程,最后返回的是mysql的临时表

话不多说,直接上代码:

CREATE DEFINER = ‘root‘@‘localhost‘
PROCEDURE myhouse.calculate_wtht_server_fee(IN store_Id VARCHAR(255), IN startDate VARCHAR(30), IN endDate VARCHAR(30))
BEGIN
    DECLARE i int DEFAULT 1;
    DECLARE v_heTongBian varchar(50);
    DECLARE v_wordType varchar(1);
    DECLARE v_albsz varchar(1);
    DECLARE v_bcChoinceTwo varchar(30);
    DECLARE v_serviceMiddleMoney int DEFAULT 0;
    DECLARE v_zhuanRangPriceT int DEFAULT 0;
    DECLARE v_bcChoinceNote11 varchar(10);
    DECLARE v_jdr varchar(10);

    DECLARE temp_count int;
    declare tmp_sum int default 0;

    DECLARE lp_flag boolean DEFAULT TRUE;

    DECLARE wtht_cursor CURSOR FOR SELECT heTongBian,wordType,albsz,bcChoinceTwo,serviceMiddleMoney,zhuanRangPriceT,bcChoinceNote11,jdr FROM wtht WHERE jdr in (select xm from sys_yh where find_in_set(StoreID, store_Id) and (isDel <> -1 or isDel is null)) AND creatTime >= startDate AND creatTime < endDate and wordType<>6 and wordType<>5 and (flag=3 or flag=-3) order by heTongBian,wordType desc;
    
      -- handler 句柄
  	DECLARE continue handler for NOT FOUND set lp_flag = false;
  
    drop table if exists temp_wtht_fee;
      -- 创建临时表收集数据
  	CREATE temporary TABLE `temp_wtht_fee` (
  		`agentName` varchar(20) COMMENT ‘经纪人姓名‘,
  		`secondContractTotal` int COMMENT ‘二手买卖签单业绩‘,
  		`secondSignNum` int COMMENT ‘签单数量‘
  	) ENGINE=InnoDB;	

    OPEN wtht_cursor;

    WHILE lp_flag DO
        fetch wtht_cursor into v_heTongBian,v_wordType,v_albsz,v_bcChoinceTwo,v_serviceMiddleMoney,v_zhuanRangPriceT,v_bcChoinceNote11,v_jdr;
        SELECT COUNT(*) INTO temp_count FROM temp_wtht_fee WHERE agentName=v_jdr;
        IF temp_count=0 THEN
            INSERT INTO temp_wtht_fee VALUES (v_jdr,0,0);
        end if;
        IF i%3<>0 THEN
            SET i = i + 1;
            if v_wordType = 4 then
              if v_bcChoinceTwo like ‘%⑨%‘ then
                set tmp_sum = tmp_sum + 3000;
              end if;
              if v_albsz = 3 then
                if v_bcChoinceTwo like ‘%⑧%‘ then
                  set tmp_sum = tmp_sum + v_zhuanRangPriceT*0.005;
                  fetch wtht_cursor into v_heTongBian,v_wordType,v_albsz,v_bcChoinceTwo,v_serviceMiddleMoney,v_zhuanRangPriceT,v_bcChoinceNote11,v_jdr;
                  set i = i + 1;
                  set tmp_sum = tmp_sum + v_serviceMiddleMoney;
                elseif v_bcChoinceTwo like ‘%?%‘ then
                  set tmp_sum = tmp_sum + v_bcChoinceNote11;
                  fetch wtht_cursor into v_heTongBian,v_wordType,v_albsz,v_bcChoinceTwo,v_serviceMiddleMoney,v_zhuanRangPriceT,v_bcChoinceNote11,v_jdr;
                  set i = i + 1;
                  set tmp_sum = tmp_sum + v_serviceMiddleMoney;
                else
                  fetch wtht_cursor into v_heTongBian,v_wordType,v_albsz,v_bcChoinceTwo,v_serviceMiddleMoney,v_zhuanRangPriceT,v_bcChoinceNote11,v_jdr;
                  set i = i + 1;
                  set tmp_sum = tmp_sum + v_serviceMiddleMoney;
                end if;
              else
                  fetch wtht_cursor into v_heTongBian,v_wordType,v_albsz,v_bcChoinceTwo,v_serviceMiddleMoney,v_zhuanRangPriceT,v_bcChoinceNote11,v_jdr;
                  set i = i + 1;
                  set tmp_sum = tmp_sum + v_serviceMiddleMoney;
              end if;
            elseif v_wordType = 1 then
              set tmp_sum = tmp_sum + v_serviceMiddleMoney;
            end if;
        ELSE
            SET i = 1;
            set tmp_sum = tmp_sum + v_serviceMiddleMoney;
            UPDATE temp_wtht_fee set secondSignNum=secondSignNum+1,secondContractTotal=secondContractTotal+tmp_sum WHERE agentName=v_jdr;
            set tmp_sum = 0;
        END IF;
     END WHILE;

    CLOSE wtht_cursor;

END

  我这个也属于现学现卖,为了怕以后忘,所以分享出来共同学习。

mysql数据库存储过程

上一篇:php&mysql的数据删除 ajax


下一篇:SQL优化案例(执行计划固定)