salix/db/routines/vn/procedures/travel_doRecalc.sql

35 lines
804 B
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`travel_doRecalc`()
proc: BEGIN
/**
* Recounts the number of entries of changed travels.
*/
DECLARE vTravelFk INT;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
DO RELEASE_LOCK('vn.ticket_doRecalc');
END;
IF !GET_LOCK('vn.travel_doRecalc', 0) THEN
LEAVE proc;
END IF;
CREATE OR REPLACE TEMPORARY TABLE tTravel
ENGINE = MEMORY
SELECT travelFk FROM travelRecalc;
UPDATE travel t
JOIN tTravel tt ON tt.travelFk = t.id
SET t.totalEntries = (
SELECT COUNT(e.id)
FROM entry e
WHERE e.travelFk = t.id
);
DELETE tr FROM travelRecalc tr JOIN tTravel t ON tr.travelFk = t.travelFk;
DROP TEMPORARY TABLE tTravel;
DO RELEASE_LOCK('vn.travel_doRecalc');
END$$
DELIMITER ;