这个是一个月的,实测已经通过。当然了,如果想做一年的,在加个循环就是了。哈哈,如果是小白,记得要点击函数那个从beging到end的地方复制进去,至于参数的部分,在函数创建的那个界面的下方有,填上去就完了。
CREATE DEFINER = `root`@`localhost` PROCEDURE `lims_report`.``(IN tableName varchar(20))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE startMonth int ;
DECLARE startYear int ;
DECLARE startDate VARCHAR(40);
DECLARE daysOfMonth int;
DECLARE everyDay int DEFAULT 1;
DECLARE partitionDate VARCHAR(40);
DECLARE partitionTemp VARCHAR(4000);
SET partitionTemp = CONCAT('ALTER TABLE ', tableName ,' partition BY RANGE(TO_DAYS('你的间隔日期,可以写为参数,灵活点'))(');
SET startMonth = 10 ;
SET startYear = 2017;
SET startDate = CONCAT(startYear,'-',startMonth,'-01');
SELECT DAYOFMONTH(LAST_DAY(startDate)) INTO daysOfMonth; -- 获取当月天数
WHILE everyDay <= daysOfMonth DO
SET partitionDate = CONCAT(startYear,'-',startMonth,'-',everyDay);
SET partitionTemp = CONCAT(partitionTemp,'PARTITION part_',startYear,startMonth,everyDay, ' VALUES LESS THAN (TO_DAYS(',"'",partitionDate,"'",'))ENGINE = InnoDB,');
SET everyDay = everyDay + 1;
END WHILE;
SET partitionTemp = SUBSTRING_INDEX(partitionTemp,',',everyDay-1);
SET partitionTemp = CONCAT(partitionTemp,');');
-- SELECT partitionTemp; -- 这个是做打印测试,没什么作用
SET @v_sql=partitionTemp; -- 注意很重要,将连成成的字符串赋值给一个变量(可以之前没有定义,但要以@开头)
PREPARE stmt FROM @v_sql; -- 预处理需要执行的动态SQL,其中stmt是一个变量
EXECUTE stmt; -- 执行SQL语句
DEALLOCATE PREPARE stmt;
en
分区移除
alter table city_part remove partitioning;移除全部分区 不会删除数据
alter table tableName drop partition partitionName 删除分区同时删除数据