What is Cursor in SQL Server ?
A SQL cursor is a database object which is used to fetch record (row) from a result set one row at a time. A SQL cursor is used when the data needs to be updated row by row.
Need to follow steps
while creating cursor
1. Declaring Cursor
A cursor is declared by defining the SQL statement.
2. Opening Cursor
A cursor is opened for storing data retrieved from the result set.
3. Fetching Cursor
When a cursor is opened, rows can be fetched from the cursor one by one.
4. Closing Cursor
The cursor should be closed explicitly after data manipulation.
5. Deallocating Cursor
Cursors should be deallocated to delete cursor definition and release all the
system resources associated with the cursor.
Example of Cursor with Variable :
Limitation of Cursors :
A cursor occupies more memory from your
system.
Cursors are faster than while loop but may be
complex.
Cursor is time consuming process because it fetches
record row by row at a time.
Cursor
are rarely suggest to use because it
takes more time to completion and can slow down the things.
Replacement of Cursors :
We can replace cursor by SQL Joins in SQL Server.