FUNCTION Retail_price_by_date( Art IN Supermag.smPriceHistory.Article%TYPE, SL IN supermag.smPriceHistory.StoreLoc%TYPE, prType IN supermag.smPriceHistory.PriceType%TYPE, DT IN supermag.smPriceHistory.EventTime%TYPE ) RETURN NUMBER IS Tmpvar NUMBER; BEGIN Tmpvar := 0; BEGIN SELECT Price INTO Tmpvar FROM Smpricehistory WHERE StoreLoc = SL AND Article = Art AND Pricetype = PrType AND Eventtime = ( SELECT MAX( EventTime ) FROM smPriceHistory WHERE StoreLoc = SL AND Article = Art AND PriceType = prType AND EventTime <= DT ); EXCEPTION WHEN NO_DATA_FOUND THEN SELECT Price INTO Tmpvar FROM Smpricehistory WHERE StoreLoc = SL AND Article = Art AND Pricetype = PrType AND Eventtime = ( SELECT MIN( EventTime ) FROM smPriceHistory WHERE StoreLoc = SL AND Article = Art AND PriceType = prType AND EventTime >= DT ); END; RETURN Tmpvar; END Retail_price_by_date;