• mysql存储过程


    -- 创建清除过期积分存储过程
    DELIMITER //
    DROP PROCEDURE IF EXISTS reportUrl //

    CREATE PROCEDURE reportUrl()

    BEGIN -- 定义变量
    DECLARE s INT DEFAULT 0;
    DECLARE sum_integral VARCHAR(255);
    DECLARE integral_Old VARCHAR(255);
    DECLARE integral_Now VARCHAR(255);
    DECLARE opid VARCHAR(256);


    -- 定义游标,并将sql结果集赋值到游标中
    DECLARE report CURSOR FOR
    -- 查询过期积分(过期日期获取总积分-用户消耗总积分)


    SELECT SUM(integral) AS sum_integral,openid FROM `t_integral_info` WHERE happen_time >='2020-01-01 00:00:00' AND happen_time <= '2020-03-03 00:00:00' GROUP BY openid;

    -- 声明当游标遍历完后将标志变量置成某个值
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET s=1;
    DROP TABLE IF EXISTS tmp_table;-- 删除临时表
    truncate table t_customer_log;

    -- 创建临时表
    CREATE TEMPORARY TABLE tmp_table (
    integral_now VARCHAR(50) NOT NULL, -- 清除过期积分后的用户积分
    integral_old VARCHAR(50) NOT NULL, -- 未清除之前的用户积分
    openid_sign VARCHAR(50) NOT NULL -- 用户 openid
    );
    -- 打开游标
    OPEN report;
    -- 将游标中的值赋值给变量,注意:变量名不要和返回的列名同名,变量顺序要和sql结果列的顺序一致
    FETCH report INTO sum_integral,opid;
    -- 当s不等于1,也就是未遍历完时,会一直循环
    WHILE s<>1 DO
    -- 执行业务逻辑
    SELECT my_integral INTO integral_Old FROM t_customer WHERE openid=opid;

    -- 当用户有效积分小于0,则使有效积分等于0
    IF sum_integral<0
    THEN
    SET sum_integral=0;

    END IF;
    UPDATE t_customer SET my_integral=sum_integral WHERE openid=opid;
    SELECT my_integral INTO integral_Now FROM t_customer WHERE openid=opid;

    -- 数据插入临时表
    INSERT INTO t_customer_log (integral_now,integral_old,openid,insert_date) VALUES(integral_Now, integral_Old,opid,now());
    -- 将游标中的值再赋值给变量,供下次循环使用
    FETCH report INTO sum_integral,opid;
    -- 当s等于1时表明遍历以完成,退出循环
    END WHILE;
    -- 关闭游标
    CLOSE report;

    SELECT*FROM t_customer_log;-- 查询操作用户表日志表

    END
    //
    DELIMITER;


    -- 调用清除过期积分存储过程(函数)
    -- call reportUrl()
    -- 还原用户积分
    -- update t_customer t1 left join t_customer_log t2 on t1.openid=t2.openid set t1.my_integral= t2.integral_old where t1.is_del=0

  • 相关阅读:
    WCF Server Console
    Restart IIS With Powershell
    RestartService (recursively)
    Copy Files
    Stopping and Starting Dependent Services
    多线程同步控制 ManualResetEvent AutoResetEvent MSDN
    DTD 简介
    Using Powershell to Copy Files to Remote Computers
    Starting and Stopping Services (IIS 6.0)
    java中的NAN和INFINITY
  • 原文地址:https://www.cnblogs.com/lhboke/p/12401171.html
Copyright © 2020-2023  润新知