> 数据库 > MySQL >

mysql 存储过程实现历史报警信息去重(只粘贴代码,不解释了)

BEGIN
        DECLARE f1, f2, f3, f4, f5 ,f6 INT;
        DECLARE Done INT DEFAULT 0;
DECLARE f11, f12, f13, f14, f15 ,f16 INT;
DECLARE result int DEFAULT 0;
DECLARE temp, temp1, temp2, temp3, temp4, temp5, temp6  int DEFAULT 0;
DECLARE record varchar(1000) DEFAULT  '0';
DECLARE i int default 0;
DECLARE cur_alarm CURSOR FOR SELECT Fault0, Fault1, Fault2, Fault3, Fault4, Fault5 from bsmu_alarm where SaveTime like concat(time, '%' ) and rtu_id = id order by SaveTime asc;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN cur_alarm;
FETCH cur_alarm INTO f1, f2, f3, f4, f5,f6;
FETCH cur_alarm INTO f11, f12, f13, f14, f15,f16;
IF (f1+f2+f3+f4+f5+f6) > 0 THEN
while (i < 6 )  do
IF (i = 0) THEN SET 
temp = f1;
END IF; 
IF (i =1 ) THEN SET 
temp = f2;
END IF;
IF (i =2 ) THEN SET 
temp = f3;
END IF;
IF (i =3 ) THEN SET 
temp = f4;
END IF;
IF (i =4 ) THEN SET 
temp = f5;
END IF;
IF (i =5 ) THEN SET 
temp = f6;
END IF;
SET record = CONCAT(record, temp); 
IF temp & 0x8000  THEN
SET result = result + 1;
END IF;
IF temp & 0x4000  THEN
SET result = result + 1;
END IF;
IF (temp & 0x2000 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x1000 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0800 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0400 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0200 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0100 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0080 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0040 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0020 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0010 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0008 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0004 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0002 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0001 ) THEN
SET result = result + 1;
END IF;
SET i = i + 1;
END WHILE;
END IF;
SET i = 0;
alarm_loop: LOOP
IF done=1 THEN
LEAVE alarm_loop;
END IF;
IF (f1 = f11)  and (f2 =f12 ) and (f3 = f13) and (f4 = f14 ) and (f5 = f15) and (f6 = f16 )THEN
SET f1 = f11;
SET f2 = f12;
SET f3 = f13;
SET f4 = f14;  
SET f5 = f15;
SET f6 = f16;
FETCH cur_alarm INTO f11, f12, f13, f14, f15,f16;
SET record = CONCAT(record, '=' ); 
ELSE
while (  i < 6 ) do
IF (i = 0 ) THEN
SET temp =  (~f1 & f11); 
END IF;
IF (i = 1 ) THEN
SET temp =  (~f2 & f12); 
END IF;
IF (i = 2 ) THEN
SET temp =  (~f3 & f13 ); 
END IF;
IF (i = 3 ) THEN
SET temp =  (~f3 & f14 ); 
END IF;
IF (i = 4) THEN
SET temp =  (~f5 & f15);
END IF;
IF (i = 5 ) THEN
SET temp =  (~f6 & f16 ); 
END IF;
SET record = CONCAT(record, temp); 
IF temp & 0x8000  THEN
SET result = result + 1;
END IF;
IF temp & 0x4000  THEN
SET result = result + 1;
END IF;
IF (temp & 0x2000 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x1000 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0800 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0400 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0200 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0100 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0080 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0040 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0020 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0010 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0008 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0004 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0002 ) THEN
SET result = result + 1;
END IF;
IF (temp & 0x0001 ) THEN
SET result = result + 1;
END IF;
SET i = i + 1;
END WHILE;
SET i = 0;
SET f1 = f11;
SET f2 = f12;
SET f3 = f13;
SET f4 = f14;  
SET f5 = f15;
SET f6 = f16;
FETCH cur_alarm INTO f11, f12, f13, f14, f15,f16;
END IF;
END LOOP alarm_loop;
CLOSE cur_alarm;
select time, result, record;
END
 

(责任编辑:IT)