ho risolto facendo così:
BEGIN
declare...

DECLARE cur CURSOR FOR SELECT x, y FROM database.tabella1
WHERE ((residuo<lottominp) AND (idcodice not in
(SELECT idcodice FROM database.tabella2 WHERE stato='aperto')));

DECLARE CONTINUE HANDLER FOR NOT FOUND SET fatto=true;

INSERT INTO...

OPEN cur;
read_loop:LOOP
FETCH cur INTO a,bo;
IF (fatto) THEN LEAVE read_loop; END IF;
INSERT INTO database.tabella2 (...)
VALUES (...);
END LOOP;

CLOSE cur;

END$


Grazie comunque a tutti