DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`business_beforeUpdate` BEFORE UPDATE ON `business` FOR EACH ROW BEGIN DECLARE isOverlapping BOOL; SET NEW.editorFk = account.myUser_getId(); IF NEW.ended IS NULL THEN SET NEW.payedHolidays = 0; END IF; IF !(OLD.started <=> NEW.started AND OLD.ended <=> NEW.ended) THEN SELECT COUNT(*) > 0 INTO isOverlapping FROM business b WHERE (util.hasDateOverlapped( NEW.started, IFNULL(NEW.ended, b.started), b.started, IFNULL(b.ended, NEW.started)) OR (NEW.ended <=> NULL AND b.ended <=> NULL)) AND b.id <> OLD.id AND workerFk = OLD.workerFk; IF isOverlapping THEN CALL util.throw ('IS_OVERLAPPING'); END IF; END IF; END$$ DELIMITER ;