| Guest Author: honey rana 27 May 2012 |
thanks ,,its great..
|
| Author: Vinod Kumar Sahu 12 Feb 2013 | Member Level: Silver Points : 8 |
What is a cursor? Cursor is a variable in SQL Server Database which is used for row-by row operations. The cursor is so named because it indicates the current position in the resultset.
Let us look this example. In this example we have 2 columns from a table and passing through the cursor and printing them.
CREATE PROCEDURE Usp_cursor_test AS BEGIN –Declaring the variables needed for cursor to store data DECLARE @Name VARCHAR(50) DECLARE @EmptypeID INT –Declaring the Cursor cur_print For name and Emptypeid in the Employeedetails table DECLARE cur_print CURSOR FOR SELECT name, emptypeid FROM employee.employeedetails –After declaring we have to open the cursor OPEN cur_print –retreives the First row from cursor and storing it into the variables. FETCH NEXT FROM cur_print INTO @Name, @EmptypeID
– @@FETCH_STATUS returns the status of the last cursor FETCH statement issued against – any cursor currently opened by the connection. – @@FETCH_STATUS = 0 means The FETCH statement was successful. – @FETCH_STATUS = -1 The FETCH statement failed or the row was beyond the result set. – @@FETCH_STATUS = -2 The row fetched is missing. WHILE @@FETCH_STATUS = 0 BEGIN –Operations need to be done,Here just printing the variables PRINT @Name PRINT @EmptypeID –retreives the NExt row from cursor and storing it into the variables. FETCH NEXT FROM cur_print INTO @Name, @EmptypeID END –Closing the cursor CLOSE cur_print – removes the cursor reference and relase cursor from memory – very Important DEALLOCATE cur_print END Note: Once cursor is opened we have to close the cursor After the usage cursor should be deallocated from the memory.
|
| Author: srirama 03 Apr 2013 | Member Level: Gold Points : 4 |
Hi saranya,
I read your article.As you mentioned about cursors.But at the end of the article 'Cursors are the SLOWEST way to access data inside SQL-Server.And Cursor is thirty times slower than set based alternatives'.is wrong interpretation/Analysis.Cursors are slow when you are using other than 'Fast-forward' option.If you have a scenario to fetch row by row data and move fast-forward direction.Cursors are more efficient than while loop.Because the cursors are pointing out to next record.And it will not allocate any memory to move backward or scrollable.
|
| Author: srirama 03 Apr 2013 | Member Level: Gold Points : 4 |
Hi saranya,
I read your article.As you mentioned about cursors.But at the end of the article 'Cursors are the SLOWEST way to access data inside SQL-Server.And Cursor is thirty times slower than set based alternatives'.is wrong interpretation/Analysis.Cursors are slow when you are using other than 'Fast-forward' option.If you have a scenario to fetch row by row data and move fast-forward direction.Cursors are more efficient than while loop.Because the cursors are pointing out to next record.And it will not allocate any memory to move backward or scrollable. read the below article http://www.dotnetspider.com/resources/44228-Cursors-its-usage.aspx
|