DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`currentRate`(vCurrencyFk INT, vDated DATE)
	RETURNS decimal(10,4)
	NOT DETERMINISTIC
	READS SQL DATA
BEGIN

	DECLARE curRate DECIMAL(10,4);
    
    SELECT value INTO curRate
		FROM referenceRate
        WHERE dated <= vDated
			AND currencyFk = vCurrencyFk
        ORDER BY dated DESC
        LIMIT 1;

RETURN curRate;

END$$
DELIMITER ;
