我尝试使用'declare'声明变量,但它总是引发错误,我不知道为什么。
代码:
mysql> DELEMITER //
mysql> CREATE PROCEDURE emp_age_count(IN age1 int,IN age2 int,OUT count INT)
-> BEGIN
-> DECLARE temp FLOAT;
-> DECLARE emp_age CURSOR FOR SELECT empage FROM emp;
-> DECLARE EXIT HANDLER FOR NOT FOUND
-> CLOSE emp_age;
-> SET @sum=0;
-> SELECT COUNT(*) INTO count FROM emp
-> WHERE empage>age1 AND empage<age2;
-> OPEN emp_age;
-> REPEAT
-> FETCH emp_age INTO temp;
-> IF temp>age1 AND temp<age2;
-> THEN SET @sum=@sum+temp;
-> END IF;
-> UNTIL 0 END REPEAT;
-> CLOSE emp_age;
-> END//
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near ';
THEN SET @sum=@sum+temp;
END IF;
UNTIL 0 END REPEAT;
CLOSE emp_age;
END' at line 13
答案 0 :(得分:1)
请删除“;”在'IF'条件结束时
应该是
IF (temp>age1 AND temp<age2)
THEN
SET @sum=@sum+temp;
END IF;