Tim D writes "I'm running SQLServer 2000 on a Win2K Server box.I have two tables: Invoice and ExchangeRateI want to store the exchange rate on the Invoice itself, by finding the corresponding exchange rate for the Invoice date. There are two problems here:1. The only link between Invoice and ExchangeRate is WHERELEFT(CONVERT(varchar, Invoice.dOrdered, 101), 10) = LEFT(CONVERT(varchar, Exchange.dDate, 101), 10)2. There are missing exchange rate entries (for weekends)This was my first approach, but it ran for over an hour before I killed it in frustration. btw, there are 300,000 invoices.declare @cur cursordeclare @date smalldatetimedeclare @rate realset @cur = cursorfor select distinct LEFT(CONVERT(varchar, dOrdered, 101), 10) as dOrdered from Invoice where dordered is not nullopen @curfetch next from @cur into @datewhile @@fetch_status = 0begin -- using top 1 to get the nearest exchange rate if there wasn't an entry on that date select TOP 1 ddate, rCADExchange from tblCADExchange where left(convert(varchar, dDate, 101), 10) <= @date order by ddate desc update invoice set rCADExchange = @rate where LEFT(CONVERT(varchar, dOrdered, 101), 10) = @date and rCADExchange is null fetch next from @cur into @dateendclose @curdeallocate @curgo
Any ideas on how to do this faster? Without a cursor maybe?"