This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/resources/sql/20190328/alert_proc_statistics_config.sql
2019-03-28 14:32:22 +08:00

132 lines
3.4 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

drop PROCEDURE proc_statistics_config;
CREATE DEFINER=`root`@`%` PROCEDURE `proc_statistics_config`()
BEGIN
DECLARE ntime VARCHAR(40);/*当前时间*/
DECLARE otime VARCHAR(40);/*上次统计时间*/
DECLARE nRow VARCHAR(40);/*本次统计条数*/
DECLARE tabName VARCHAR(500);
DECLARE description VARCHAR(500);
DECLARE deleteSql VARCHAR(500);
DECLARE done INT;/*游标标识*/
DECLARE flag INT;/*循环标识*/
DECLARE t_error INT;/*错误标识*/
DECLARE proc_log_table VARCHAR(100);/*存储过程日志表*/
DECLARE proc_name VARCHAR(100);/*存储过程名称*/
DECLARE icursor CURSOR FOR SELECT tab_name FROM statistics_tables where is_valid=1;
DECLARE CONTINUE HANDLER FOR NOT found SET done=1;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION set t_error=1;
select max(statistic_time) into otime from cfg_num_statistics;
SET done=0;
SET t_error=0;
SET proc_log_table='proc_exec_log';
SET proc_name='proc_statistics_config';
SET ntime=DATE_FORMAT(SYSDATE(),'%Y-%m-%d %H:%i:%S');
OPEN icursor;
loop_iloop:LOOP
FETCH icursor INTO tabName;
SET description=tabName;
set @descriptionStart=concat(description,'表统计start');
/*统计当前配置表数据到统计表中start*/
set @v_log_sql1 := concat('insert into ',proc_log_table,'(proc_name,table_name,log_time,description) values(?,?,?,?)');
PREPARE execs FROM @v_log_sql1;
EXECUTE execs using proc_name,proc_log_table,ntime,@descriptionStart;
DEALLOCATE PREPARE execs;
COMMIT;
#20190328 is_audit=1 && (is_valid=1 || is_valid=0) approved cfg_state=4[is_valid=0 && is_audit=1],cfg_state=1approved值实际包含cfg_state=4cfg_state=1
set @insert_statistics_sql := concat('insert into cfg_num_statistics(statistic_time,function_id,service_id,action,compile_id,cfg_state) select ','''',ntime,'''',',','function_id,service_id,action,compile_id,if(is_valid=-1,-1,if(is_valid=1,1,if(is_audit=0,0,if(is_audit=1,4,if(is_audit=2,2,if(is_audit=3,3,0)))))) cfg_state from ',tabName);
PREPARE execs FROM @insert_statistics_sql;
EXECUTE execs;
DEALLOCATE PREPARE execs;
COMMIT;
set @descriptionEnd=concat(description,'表统计end');
set @v_log_sql2 := concat('insert into ',proc_log_table,'(proc_name,table_name,log_time,description) values(?,?,?,?)');
PREPARE execs FROM @v_log_sql2;
EXECUTE execs using proc_name,proc_log_table,ntime,@descriptionEnd;
DEALLOCATE PREPARE execs;
COMMIT;
/*异常退出loop*/
IF t_error=1 THEN
LEAVE loop_iloop;
END IF;
/*循环结束退出loop*/
IF done=1 THEN
LEAVE loop_iloop;
ELSE
SET flag=0;
END IF;
IF flag=0 THEN
SET done=0;
END IF;
END LOOP loop_iloop;
CLOSE icursor;
/*取出本次统计条数*/
SELECT count(statistic_time) INTO nRow from cfg_num_statistics where statistic_time=ntime;
IF t_error=1 THEN /*如果异常清楚本次数据*/
delete from cfg_num_statistics where statistic_time=ntime;
COMMIT;
ELSEIF nRow > 0 THEN /*判断本次统计是否有数据录入,如果有则删除上次统计数据,如果没有则不清除上次统计数据*/
delete from cfg_num_statistics where statistic_time=otime;
COMMIT;
END IF;
COMMIT;
END