CURSOR is a server side tool and completely different from ADO.NET’s cursor. It is giving row-by-row solution to the result set and let me tell you that SQL Server impressive with handling set of rows, not row-by-row. This is useful for those who came from procedural background and don’t much familiar with set-based relational algebra.
You can define CURSOR as read only or update but read only is fast as compare with update as read only will gives your data and won’t remember it.
Personally I used to avoid cursor as long as it is possible as it uses lots of resources of server and reduce the performance. Whenever it is possible use temp table, derived table, sub-query, CASE statement but finally you draw conclusion that any of the set-based operation won’t work for your problem than and than go for CURSOR solution but make sure you are not selecting more than necessary rows in CURSOR. Lesser the row, higher the performance!!!!
The main reason for writing this article is to help those who want to understand the concept of CURSOR and another reason CURSOR is not something that you can completely ignore. There may be some situation where you have to use CURSOR, may be some complex logic or dynamic code iteration especially while making code generator.
Type of CURSOR:
Static: This is lowest type of CURSOR and used to use for finding data and generating reports. Once getting the data into the CURSOR you will not identify any modification done in data after retrieving it because it make a copy of your data into the temp table of tempDB.
Forward_Only: This is the default type of CURSOR and really very similar to Static CURSOR the only difference you will find it, it will move forward only. In short, this CURSOR will scroll from first to last no other movement supported.
Fast_Forward: this is a mixture of Forward_Only and Read_Only.
Dynamic: this CURSOR will be bit slow as it will accept any data modification done by any user even after you fetch the data because you scroll around the CURSOR. Data membership, value and its order will be changed in each FETCH if any data modification has been done in record set.
Keyset-Driven: when you open this CURSOR, membership key and order of row are fixed in CURSOR.
Steps for CURSOR:
DECLARE: Defines a CURSOR with standard SELECT statement. This must be done before you open the CURSOR.
OPEN: physically open the CURSOR and received the record set exist in table at the time of opening the CURSOR.
FETCH: CURSOR always points to one row at a time and FETCH is retrieve the value from that row to manipulate it further.
CLOSE: CLOSE will release the lock on table made by CURSOR. If you wish than you can re-open CURSOR after closes it.
DEALLOCATE: Once you are done with CURSOR, do DEALLOCATE it and removes the memory from the server. You can open the CURSOR once you close it but can’t re-open CURSOR once you DEALLOCATE it.
You can run following query for dynamic management function in-order to get information about CURSOR running on your server.
select * from sys.dm_exec_cursors(0)
I will give examples to make your concept more clearly about different type of CURSOR in my future article.
Reference: Ritesh Shah
Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah