mysql写的存储过程的一些小问题
DELIMITER $$ USE `yzhoteldb`$$ DROP PROCEDURE IF EXISTS `yz_waveData`$$ CREATE DEFINER=`cacdba`@`%` PROCEDURE `yz_waveData`( IN devCode VARCHAR(32), IN WhereCondition VARCHAR(100) ) BEGIN DECLARE ftime VARCHAR(32); DECLARE fvalue FLOAT; -- 参与计算的变量需要写一个默值,否则值一直会变成null DECLARE sumValue FLOAT DEFAULT 0; DECLARE avgvalue FLOAT; DECLARE sqlstr VARCHAR(32767); DECLARE i INT DEFAULT 0; DECLARE done INTEGER DEFAULT 0; DECLARE cur CURSOR FOR SELECT f_time,f_value FROM this_yc201508; -- 这一句一定要写无数据时done标志为1,否则会无循环的。 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1; CREATE TEMPORARY TABLE IF NOT EXISTS avgDATA ( F_TIME VARCHAR(32), F_VALUE FLOAT ); TRUNCATE avgDATA; OPEN cur; FETCH cur INTO ftime,fvalue; WHILE (done<>1) DO SET i=i+1; SET sumValue=sumValue+fvalue; SET avgvalue=sumValue/i; INSERT INTO avgDATA(F_TIME,F_VALUE) VALUES(ftime,avgvalue); FETCH cur INTO ftime,fvalue; END WHILE; CLOSE cur; -- INSERT INTO avgDATA(F_TIME,F_VALUE) VALUES('1','11'); SELECT * FROM avgDATA; /*set sqlstr="SELECT * FROM avgDATA "; select sqlstr; IF sqlstr<>"" THEN begin SET @sqlCommand=sqlstr; 动态语句采用这种形式来写 PREPARE STMT FROM @sqlCommand; EXECUTE STMT; DEALLOCATE PREPARE STMT; end; END IF; */ END$$ DELIMITER ; -- 没有这一句的话,mysql会把这段SQL当SQL语句而不是存储过程