DROP FUNCTION IF EXISTS `vn`.`priceFixed_getRate2`; DELIMITER $$ $$ CREATE FUNCTION `vn`.`priceFixed_getRate2`(vFixedPriceFk INT, vRate3 DOUBLE) RETURNS DOUBLE BEGIN DECLARE vWarehouse INT; DECLARE vRate2 DOUBLE; SELECT round(vRate3 * (1 + ((r.rate2 - r.rate3)/100)), 2) INTO vRate2 FROM vn.rate r JOIN vn.priceFixed p ON p.id = vFixedPriceFk WHERE r.dated <= p.started AND r.warehouseFk = p.warehouseFk ORDER BY r.dated DESC LIMIT 1; RETURN vRate2; END$$ DELIMITER ;