USE `vn`; DROP procedure IF EXISTS `zone_doCalc`; DELIMITER $$ USE `vn`$$ CREATE DEFINER=`root`@`%` PROCEDURE `zone_doCalc`() proc: BEGIN /** * Updates ticket fields related with zone */ DECLARE vDone BOOL; DECLARE vTicketFk INT; DECLARE vShipped DATE; DECLARE vZoneFk INT; DECLARE cCur CURSOR FOR SELECT t.id, t.shipped, t.zoneFk FROM zoneCalcTicket zct JOIN ticket t ON t.zoneFk = zct.zoneFk WHERE shipped >= CURDATE(); DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE; OPEN cCur; myLoop: LOOP SET vDone = FALSE; FETCH cCur INTO vTicketFk, vShipped, vZoneFk; IF vDone THEN LEAVE myLoop; END IF; DROP TEMPORARY TABLE IF EXISTS tmp.zone; CREATE TEMPORARY TABLE tmp.zone (INDEX (id)) ENGINE = MEMORY SELECT vZoneFk id; CALL zone_getOptionsForShipment(vShipped, TRUE); UPDATE ticket t LEFT JOIN tmp.zoneOption zo ON TRUE SET zonePrice = zo.price, zoneBonus = zo.bonus, zoneClosure = zo.`hour` WHERE t.id = vTicketFk; END LOOP; CLOSE cCur; DELETE FROM zoneCalcTicket; END$$ DELIMITER ;