I am getting an error in the following code below. The error is #1329 - No data - zero rows fetched, selected, or processed. What exactly does this mean and what is it that i am doing incorrectly?
Thanks
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;
解决方案
You should a DECLARE HANDLER for the cursor when no more data is found: in docs you can find about how to do so.
Use
DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement
Where handler type is one of: CONTINUE, EXIT or UNDO
Condition value is one of SQLSTATE [VALUE] sqlstate_value | condition_name | SQLWARNING | NOT FOUND | SQLEXCEPTION | mysql_error_code. In this case NOT FOUND is what you're (maybe) looking for.
So your code would become something like this:
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;
Also review the docs for further information.