Hello Reader. In this post, we want to discuss a nice function for using Cursor in SQL Server. Before we get started, if you want to know about random number generators, please go through the following article: Random Number Generator Script – SQL Query.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
DECLARE @ItemCode    NVARCHAR(100) DECLARE @FileCount   NVARCHAR(100) DECLARE @DepRate   NVARCHAR(10) DECLARE @MyCursor CURSOR SET @MyCursor = CURSOR FAST_FORWARD FOR SELECT I.ItemCode, COUNT(F.ItemCode) AS FileCount, I.DepRate FROM tblItemMst_OV I INNER JOIN tblFYDepreciationDetail F ON I.ItemCode=F.ItemCode WHERE I.isClosed=0 GROUP BY I.ItemCode, I.DepRate OPEN @MyCursor FETCH NEXT FROM @MyCursor INTO @ItemCode, @FileCount, @DepRate WHILE @@FETCH_STATUS = 0 BEGIN PRINT @ItemCode+' - '+@FileCount IF (@FileCount = 1) BEGIN print '*******' --INSERT INTO tblFYDepreciationDetail(ItemCode, FYStart, FYEnd, DepFY, CostOpn, CostTot, DepRate, DepOpn, DepTot, WDV) --VALUES(@ItemCode, '232323','565656',GETDATE(), 4545,4545,@DepRate, 4545,4545,545) END FETCH NEXT FROM @MyCursor INTO @ItemCode, @FileCount, @DepRate END CLOSE @MyCursor DEALLOCATE @MyCursor |
Leave a Comment