标签:src update rom 事件 报错 control now() desc mina
1.采集系统需要将实时数据存入历史表,利用mysql的事件,每小时存一次采集数据;
2.每个月1号凌晨1点,将历史表备份,名称命名为his_aic_20190501 01:00,将原表清空。
BEGIN
INSERT INTO his_dic (ParentID,PointID,DICName,DICValue,StateDesc,AlarmThreshold,AlarmLevel,AlarmEnable,UpdatedTime)
select ParentID,PointID,DICName,DICValue,StateDesc,AlarmThreshold,AlarmLevel,AlarmEnable,UpdatedTime
FROM dic on duplicate key update
ParentID=VALUES( ParentID),PointID=VALUES(PointID ),DICName=VALUES(DICName ),DICValue=VALUES(DICValue ),StateDesc=VALUES(StateDesc ),AlarmThreshold=VALUES( AlarmThreshold),AlarmLevel=VALUES(AlarmLevel ),AlarmEnable=VALUES( AlarmEnable),UpdatedTime=VALUES(UpdatedTime );
INSERT INTO his_doc (ParentID,PointID,DOCName,DetectDOStatus,DOCValue,StateDesc,ControlEnable,UpdatedTime)
SELECT ParentID,PointID,DOCName,DetectDOStatus,DOCValue,StateDesc,ControlEnable,UpdatedTime
FROM doc on duplicate key update
ParentID=VALUES(ParentID ),PointID=VALUES(PointID ),DOCName=VALUES(DOCName ),DetectDOStatus=VALUES( DetectDOStatus),DOCValue=VALUES(DOCValue ),StateDesc=VALUES(StateDesc ),ControlEnable=VALUES( ControlEnable),UpdatedTime=VALUES(UpdatedTime );
INSERT INTO his_aic (ParentID,PointID,AICName,AICValue,Unit,sAICValue,MaxAICValue,MinAICValue,`Enable`,UpdatedTime)
SELECT ParentID,PointID,AICName,AICValue,Unit,sAICValue,MaxAICValue,MinAICValue,`Enable`,UpdatedTime
FROM aic on duplicate key update
ParentID=VALUES(ParentID ),PointID=VALUES(PointID ),AICName=VALUES(AICName ),AICValue=VALUES( AICValue),Unit=VALUES(Unit ),sAICValue=VALUES(sAICValue ),MaxAICValue=VALUES(MaxAICValue ),MinAICValue=VALUES(MinAICValue ),`Enable`=VALUES( `Enable`),UpdatedTime=VALUES(UpdatedTime);
END
步骤1 使用navicat工具管理Mysql,函数右键新建函数,在跳出得向导框选择过程,点击下一步。
步骤2 这里不需要任何操作,直接点击完成,因为没有参数传入传出。
步骤3 将上面的存储过程代码复制进如下图框,点击运行即可。
步骤4点击保存,输入名称。
结果运行结果如下。
CREATE EVENT if not exists event_To_His
on schedule every 1 hour
on completion preserve
do call To_His();
步骤1选择自己的数据,点击右键,选择cmd模式输入。
步骤2将以上代码拷贝运行即可。
结果运行结果如下。
备注:
如果报错查看event是否开启: show variables like ‘%sche%‘;
将事件计划开启: set global event_scheduler=1;
关闭事件任务: alter event e_test ON COMPLETION PRESERVE DISABLE;
开户事件任务: alter event e_test ON COMPLETION PRESERVE ENABLE;
BEGIN
create table his_aic_temp like his_aic;
set @i=now();
set @sqlstr=CONCAT('rename table his_aic to `his_aic_',cast(@i as char),'`');
select @sqlstr;
PREPARE renameHisBak FROM @sqlstr;
EXECUTE renameHisBak;
rename table his_aic_temp to his_aic;
create table his_dic_temp like his_dic;
set @i=now();
set @sqlstr=CONCAT('rename table his_dic to `his_dic_',cast(@i as char),'`');
select @sqlstr;
PREPARE renameHisBak FROM @sqlstr;
EXECUTE renameHisBak;
rename table his_dic_temp to his_dic;
create table his_doc_temp like his_doc;
set @i=now();
set @sqlstr=CONCAT('rename table his_doc to `his_doc_',cast(@i as char),'`');
select @sqlstr;
PREPARE renameHisBak FROM @sqlstr;
EXECUTE renameHisBak;
rename table his_doc_temp to his_doc;
END
详见1.2
CREATE DEFINER=`root`@`localhost`
EVENT `event_HistoryBak`
ON SCHEDULE EVERY 1 MONTH STARTS DATE_ADD(DATE_ADD(DATE_SUB(CURDATE(),INTERVAL DAY(CURDATE())-1 DAY),INTERVAL 1 MONTH),INTERVAL 1 HOUR)
ON COMPLETION PRESERVE ENABLE
DO call HistoryBak();
详见2.2
QQ群:20120449
标签:src update rom 事件 报错 control now() desc mina
原文地址:https://www.cnblogs.com/JerryMouseLi/p/10783147.html