CREATE PROCEDURE `copy_record_out`()
BEGIN
DECLARE startYear int;
DECLARE endYear int;
DECLARE startMonth int;
DECLARE endMonth int;
DECLARE startTime dateTime;
DECLARE endTime dateTime;
DECLARE middleTime dateTime;
declare startTimeStr VARCHAR(32);
declare middleTimeStr VARCHAR(32);
select 1 into startMonth;
select str_to_date('2019-12-01 00:00:00','%Y-%m-%d %H:%i:%s') into startTime;
select str_to_date('2020-01-01 00:00:00','%Y-%m-%d %H:%i:%s') into endTime;
while startTime<endTime do
select YEAR(startTime) into startYear;
select MONTH(startTime) into startMonth;
select DATE_FORMAT(startTime,'%Y-%m-%d %H:%i:%s') into startTimeStr;
select date_add(startTime, interval 1 HOUR) into middleTime;
select DATE_FORMAT(middleTime,'%Y-%m-%d %H:%i:%s') into middleTimeStr;
set @table_name = concat("t_park_record_out_archive_",startYear,"_",startMonth);
set @statement = concat("insert into ",@table_name, "(
create_time,update_time )
select create_time, update_time from t_park_record_out
where out_time>='",startTimeStr,"' and out_time<'",middleTimeStr,"';");
prepare stmt from @statement;
execute stmt;
commit;
delete from t_park_record_out where out_time>=startTime and out_time<date_add(startTime, interval 1 HOUR);
commit;
select middleTime into startTime;
select YEAR(startTime) into startYear;
select MONTH(startTime) into startMonth;
end while;
end;
mysql编写存储过程
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 1.创建存储过程: delimiter $$ create procedure pro() begin set @...
- 原文链接 MySQL存储过程详解 1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然...
- 参考官方给出的:https://dev.mysql.com/doc/refman/8.0/en/stored-ro...