What is Cursor?
A cursor is a database object used by applications to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.
In order to work with a cursor, we need to perform some steps in the following order:
- Declare cursor
- Open cursor
- Fetch row from the cursor
- Process fetched row
- Close cursor
- Deallocate cursor
This is the simplest example of the SQL Server Cursor. I have used this all the time for any use of Cursor in my T-SQL.
- DECLARE @AccountID INT
- DECLARE @getAccountID CURSOR
- SET @getAccountID = CURSOR FOR
- SELECT Account_ID
- FROM Accounts
- OPEN @getAccountID
- FETCH NEXT
- FROM @getAccountID INTO @AccountID
- WHILE @@FETCH_STATUS = 0
- BEGIN
- PRINT @AccountID
- FETCH NEXT
- FROM @getAccountID INTO @AccountID
- END
- CLOSE @getAccountID
- DEALLOCATE @getAccountID

SubashPosted Mar 17, 2017, 9:33 PM
Good keep sharing nice explanation
Pravas RanjanPosted Mar 9, 2017, 6:01 AM
Thanks for your post here...
Pravas RanjanPosted Mar 9, 2017, 6:01 AM
Good Article but please elaborate more on cursor with brief with examples...