我在下面的代码中收到错误。错误是#1329 - 无数据 - 提取,选择或处理零行。这究竟是什么意思,我做错了什么? 感谢
create procedure cursorproc(OUT p_out DECIMAL(10,2)) begin
declare l_salary, l_total DECIMAL(10,2);
declare cur_1 cursor for select line_distance from elements;
open cur_1;
set l_total = 0;
loop
fetch cur_1 into l_salary;
set l_total = l_total + l_salary;
end loop;
close cur_1;
set p_out = l_total;
end;
答案 0 :(得分:4)
当找不到更多数据时,您应该为光标DECLARE HANDLER
:在docs中,您可以找到有关如何执行此操作的信息。
使用
DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
处理程序类型是以下之一:CONTINUE, EXIT or UNDO
条件值是SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | NOT FOUND | SQLEXCEPTION | mysql_error_code
之一。在这种情况下,NOT FOUND
就是您(可能)正在寻找的。 p>
所以你的代码会变成这样:
CREATE PROCEDURE cursorproc(OUT p_out DECIMAL(10,2))
BEGIN
DECLARE l_salary, l_total DECIMAL(10,2);
DECLARE _continue INT DEFAULT 0;
DECLARE cur_1 CURSOR FOR SELECT line_distance FROM elements;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET _continue =1;
OPEN cur_1;
SET l_total = 0;
REPEAT
FETCH cur_1 INTO l_salary;
SET l_total = l_total + l_salary;
UNTIL _continue = 1;
END REPEAT;
CLOSE cur_1;
SET p_out = l_total;
END;
另请查看docs以获取更多信息。
答案 1 :(得分:1)