I have two tables :
Bill :
create table Bill(
BillDate datetime,
Amount decimal(10,2) ,
BillCurrency varchar(3) ,
ReportingCurrency decimal(10,2))
FxRate :
create table FxRate(
RateDate datetime,
SourceCurrency varchar(3),
TargetCurrency varchar(3),
ExchangeRate decimal(15,4))
This is what I want to do :
I want to update my Bill table as
update Bill
set ReportingCurrency = FxRate.ExchangeRate * Bill.Amount
from FxRate
where FxRate.RateDate = Bill.BillDate
In this update all the rows which have an entry for that particular date will get the new reportingcurrency data. Since Bill table can have multiple rows eligible for the update , I have the following problem :
For the rows where there was no entry in FxRate table (for that date), the ReportingCurrency becomes NULL. I want to go back to the nearest <= RateDate and pick up the exchange rate.Is that possible using modifications in the same update statement or some other efficient method? (I want to avoid a cursor).