CREATE PROCEDURE `p_preserve_his`(in months int)
BEGIN
Declare lastMonth date;
Declare lastdt int;
Declare statisdate int;
DECLARE i int;
DECLARE loop_index int DEFAULT 0;
DECLARE done boolean DEFAULT FALSE;
DECLARE need_delete boolean DEFAULT TRUE;
DECLARE cur CURSOR FOR select distinct dt from $table_name order by dt DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH next from cur INTO statisdate;
#结束循环
IF done THEN
LEAVE read_loop;
END IF;
set loop_index = loop_index + 1;
#最大日期
if loop_index = 1 then
ITERATE read_loop;
end if;
#设置一个标记
outer_label: BEGIN
set
MySql存储过程:保留每月末数据及最新一版数据
最新推荐文章于 2024-07-20 15:23:05 发布