DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`collection_make`() proc:BEGIN /** * Genera colecciones de tickets sin asignar trabajador a partir de la tabla * vn.collectionHotbed. */ DECLARE vUserFk INT; DECLARE vCounter INT; DECLARE vMaxCollectionWithoutUser INT; DECLARE vHasEnoughFreeCollections BOOL; DECLARE vDone INT DEFAULT FALSE; DECLARE cur1 CURSOR FOR SELECT DISTINCT userFk FROM vn.collectionHotbed; DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; SELECT maxCollectionWithoutUser INTO vMaxCollectionWithoutUser FROM vn.productionConfig pc; SELECT (vMaxCollectionWithoutUser - COUNT(*)) <= 0 INTO vHasEnoughFreeCollections FROM vn.collection c JOIN vn.operator o ON o.numberOfWagons = c.wagons AND o.trainFk = c.trainFk AND o.itemPackingTypeFk = c.itemPackingTypeFk WHERE c.workerFk IS NULL AND o.workerFk = vUserFk; IF vHasEnoughFreeCollections THEN LEAVE proc; END IF; OPEN cur1; read_loop: LOOP SET vDone = FALSE; FETCH cur1 INTO vUserFk; IF vDone THEN LEAVE read_loop; END IF; SET vCounter = vMaxCollectionWithoutUser; WHILE vCounter > 0 DO SET vCounter = vCounter - 1; CALL vn.collection_new(vUserFk, @vCollectionFk); END WHILE; DELETE FROM vn.collectionHotbed WHERE userFk = vUserFk; END LOOP; CLOSE cur1; END$$ DELIMITER ;