获取游标时出错

时间:2015-12-05 08:52:13

标签: mysql cursor

我正在处理这个光标,它获取一个表的工资列,看看一个值是否大于2500,我们应该取500作为税,而ELSE我们应该只取200,最后选择并显示一个表税后工资的新栏目:

CREATE DEFINER=`root`@`localhost` PROCEDURE `tax_to_salary`()
BEGIN
DECLARE basic_salary INTEGER;
DECLARE new_salary INTEGER;
DECLARE cur1 CURSOR FOR SELECT salary FROM employee;
OPEN cur1;
l1:LOOP
FETCH cur1 INTO basic_salary;
IF basic_salary>2500 THEN
SET @new_salary := 500;
SET @basic_salary := @basic_salary - @new_salary;
else
SET @new_salary := 200;
SET @basic_salary := @basic_salary - @new_salary;
END IF;
END LOOP;
SELECT emp_name, salary, basic_salary AS 'Salary after taxes' FROM employee; 
END

我收到了这个错误:

  

选取或处理的零行

1 个答案:

答案 0 :(得分:1)

了解退出处理程序和继续处理程序  MySQL handlers

尝试以下内容:

CREATE DEFINER=`root`@`localhost` PROCEDURE `tax_to_salary`()
BEGIN
DECLARE basic_salary INTEGER;
DECLARE new_salary INTEGER;
DECLARE cur1 CURSOR FOR SELECT salary FROM employee;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur1;
l1:LOOP
FETCH cur1 INTO basic_salary;
IF basic_salary>2500 THEN
SET @new_salary := 500;
SET @basic_salary := @basic_salary - @new_salary;
else
SET @new_salary := 200;
SET @basic_salary := @basic_salary - @new_salary;
END IF;
END LOOP;
SELECT emp_name, salary, basic_salary AS 'Salary after taxes' FROM employee; 
END