Dear all,
I write few lines to do a bottom-up calculation, with 'fetch last' and 'fetch prior'.
It seems that the condition 'WHILE @@FETCH_STATUS = 0' does not work when cursor arrives at the first line, as there is an error message:
'(1 row(s) affected)
6255.84
(1 row(s) affected)
Msg 16931, Level 16, State 1, Line 18
There are no rows in the current fetch buffer.
The statement has been terminated.'
Can anyone kindly tell me how to fix the error? Thanks.
Here is my code:
DECLARE @lastprice real
DECLARE @updatedprice real
DECLARE @updatedRe real
DECLARE @updatedAUX real
SET @lastprice = (
SELECT Close_P from #ClosePrice where #ClosePrice.DateTD = (SELECT MAX(#ClosePrice.DateTD) FROM #ClosePrice)
)
SET @updatedprice = @lastprice;
DECLARE date_cursor SCROLL CURSOR
FOR SELECT Re_adj from #ClosePrice ORDER BY DateTD;
OPEN date_cursor;
FETCH LAST FROM date_cursor INTO @updatedRe;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @updatedprice = @updatedprice / (1 + @updatedRe);
PRINT @updatedprice;
FETCH PRIOR FROM date_cursor INTO @updatedRe;
UPDATE #ClosePrice SET Close_P_adj = @updatedprice WHERE CURRENT OF date_cursor;
END
CLOSE date_cursor;
DEALLOCATE date_cursor;
UPDATE #ClosePrice
SET #ClosePrice.Close_P_adj = @lastprice
WHERE #ClosePrice.DateTD = (SELECT MAX(#ClosePrice.DateTD) FROM #ClosePrice)
go
- Edited by Bobojin_Z 21 hours 44 minutes ago