DELIMITER $$ CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemMinimumQuantity_check`( vSelf INT, vItemFk INT, vStarted DATE, vEnded DATE, vWarehouseFk INT ) BEGIN DECLARE vHasCollision BOOL; IF vStarted IS NULL THEN CALL util.throw('The field "started" cannot be null'); END IF; SELECT COUNT(*) INTO vHasCollision FROM itemMinimumQuantity WHERE vItemFk = itemFk AND ((vStarted <= ended OR ended IS NULL) AND (vStarted >= `started` OR vEnded IS NULL)) AND (vWarehouseFk <=> warehouseFk) AND vSelf <> id; IF vHasCollision THEN CALL util.throw('A line with the same configuration already exists'); END IF; END$$ DELIMITER ;