实现按年月建表(每个月3张表),输入的两个参数分表是开始年份与结果年份(包含本输入的那个年份)
CREATE DEFINER=`root`@`localhost` PROCEDURE `2019,2030`(IN `yearStart` int,IN `yearEnd` int) BEGIN #Routine body goes here... DECLARE tableName VARCHAR(32); DECLARE mon varchar(2); DECLARE i int; DECLARE j int; set j=0; set i=1; while yearStart<=yearEnd DO while i<13 DO if i<10 THEN set mon=CONCAT(‘0‘,i); ELSE set mon=CONCAT(i,‘‘); end if; set tableName=CONCAT(`yearStart`,mon); while j<3 DO set @d=concat(‘DROP TABLE IF EXISTS t_api_log_‘,tableName,‘_‘,j,‘;‘); prepare del from @d; execute del; SET @s=CONCAT(‘CREATE TABLE t_api_log_‘,tableName,‘_‘,j,‘ ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `type` int(11) DEFAULT NULL COMMENT \‘枚举(0:top订购请求,1:top退订请求,2:请求boss订购,3:请求boss退订;4:boss请求订购,5:boss请求退订)\‘, `create_time` datetime DEFAULT NULL, `user_id` varchar(50) DEFAULT NULL, `req_info` varchar(255) DEFAULT NULL, `resp_info` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;‘); prepare stmt from @s; execute stmt; set j=j+1; end while; set j=0; set i=i+1; end while; set i=1; set yearStart=yearStart+1; end while; END
原文:https://www.cnblogs.com/chaojibaidu/p/10871328.html