I have a requirement where I need to insert multiple rows to my currency table which consists of three columns (date, currency, ex-rate) and three months of data.
NOW I need to add six more months of data to the table where the currency and ex-rate columns are constant and only change is date column. I have to maintain 150 currencies on each date.
Are you absolutely sure you want to add aprox. 27000 records to your table?
I would first investigate if it is possible to change your existing queries, handling the currency table as it is (possibly creating a function/procedure to look up the exchange rate at a given date).
Yes, I have to insert 6 months data to my currency table where the only change will date.
I am looking for the best possible way to implement this whether to use stored proc or to use any variable.
The table contains 3 months of data i.e from Jan 2015 to Mar 2015 with 3 columns Date Currency and Ex-rate.
Now i need to add 6 months of data i.e from April to Sep where the currency and ex-rate remains constant and the date varies.
Hope you have understood my query. Can I you help me on this.
declare @fromdate date='20150401'
,@todate date='20150930'
;
with cte(thedate)
as (select @fromdate as thedate
union all
select dateadd(dd,1,thedate) as thedate
from cte
where thedate<@todate
)
insert into curency_rate([date],cur,[ex-rate])
select b.thedate
,a.cur
,a.[ex-rate]
from (select *
,row_number() over(partition by cur order by [date] desc) as rn
from curency_rate
) as a
cross join cte as b
where a.rn=1
option(maxrecursion 0)
;