The PREPARE statement must follow the DECLARE CURSOR that references it.
Move the DECLARE CURSOR statement before the PREPARE statement, then recompile the program.