MySQL過程和遊標

做你的貓發表於2018-09-21
BEGIN
    DECLARE f_leastCount INT DEFAULT 100;
    DECLARE f_ratio FLOAT DEFAULT 0.8;

    DECLARE i_channel VARCHAR(64);
    DECLARE i_appVersionName VARCHAR(64);
    DECLARE i_statDate DATE;
    DECLARE i_accumulateCount INT;
    DECLARE i_activateCount INT;
    DECLARE i_dau30Count INT;
    DECLARE i_dau7Count INT;
    DECLARE i_dauCount INT;
    DECLARE i_dauImeiCount INT;
    DECLARE i_dauImsiCount INT;
    DECLARE i_launchCount INT;
    DECLARE i_lostCount INT;
    DECLARE i_newCount INT;

    #遍歷資料結束標誌
  DECLARE done INT DEFAULT FALSE;

  #遊標
  DECLARE cur CURSOR FOR SELECT ds.channel,ds.appVersionName,ds.statDate,ds.accumulateCount,ds.activateCount,ds.dau30Count,ds.dau7Count,ds.dauCount,ds.dauImeiCount,ds.dauImsiCount,ds.launchCount,ds.lostCount,ds.newCount FROM appstore_dau_stat ds WHERE ds.statDate = DATE_SUB(CURDATE(),INTERVAL m_daysdelay DAY);
    #將結束標誌繫結到遊標
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    -- 開啟遊標
    OPEN cur;
    -- 開始迴圈
  read_loop: LOOP
        -- 提取遊標裡的資料
        FETCH cur INTO i_channel, i_appVersionName, i_statDate, i_accumulateCount, i_activateCount, i_dau30Count, i_dau7Count, i_dauCount, i_dauImeiCount, i_dauImsiCount, i_launchCount, i_lostCount, i_newCount;
        -- 宣告結束的時候
    IF done THEN
      LEAVE read_loop;
    END IF;
        -- 事務處理
        IF i_accumulateCount > f_leastCount THEN
            SET i_accumulateCount = ROUND(i_accumulateCount * f_ratio);
        END IF;
        IF i_activateCount > f_leastCount THEN
            SET i_activateCount = ROUND(i_activateCount * f_ratio);
        END IF;
        IF i_dau30Count > f_leastCount THEN
            SET i_dau30Count = ROUND(i_dau30Count * f_ratio);
        END IF;
        IF i_dau7Count > f_leastCount THEN
            SET i_dau7Count = ROUND(i_dau7Count * f_ratio);
        END IF;
        IF i_dauCount > f_leastCount THEN
            SET i_dauCount = ROUND(i_dauCount * f_ratio);
        END IF;
        IF i_dauImeiCount > f_leastCount THEN
            SET i_dauImeiCount = ROUND(i_dauImeiCount * f_ratio);
        END IF;
        IF i_dauImsiCount > f_leastCount THEN
            SET i_dauImsiCount = ROUND(i_dauImsiCount * f_ratio);
        END IF;
        IF i_launchCount > f_leastCount THEN
            SET i_launchCount = ROUND(i_launchCount * f_ratio);
        END IF;       
        IF i_lostCount > f_leastCount THEN
            SET i_lostCount = ROUND(i_lostCount * f_ratio);
        END IF;       
        IF i_newCount > f_leastCount THEN
            SET i_newCount = ROUND(i_newCount * f_ratio);
        END IF;

        IF NOT EXISTS (SELECT id FROM appstore_dau_stat_temp WHERE channel = i_channel AND appVersionName = i_appVersionName AND statDate = i_statDate) THEN
            INSERT INTO appstore_dau_stat_temp(channel,appVersionName,statDate,accumulateCount,activateCount,dau30Count,dau7Count,dauCount,dauImeiCount,dauImsiCount,launchCount,lostCount,newCount)
                VALUES(i_channel, i_appVersionName, i_statDate, i_accumulateCount, i_activateCount, i_dau30Count, i_dau7Count, i_dauCount, i_dauImeiCount, i_dauImsiCount, i_launchCount, i_lostCount, i_newCount);
        END IF;

  END LOOP;
    -- 關閉遊標
    CLOSE  cur;

END

  

相關文章