What is cursor in SQL Server?

What is cursor in SQL Server?

- A Cursor is a database object that represents a result set and is used to manipulate data row by row.
- When a cursor is opened, it is positioned on a row and that row is available for processing.
- SQL Server supports three types of cursor namely Transact-SQL server cursor, API server cursor, and client cursor.
- Transact-SQL Server cursors use Transact-SQL statements and are declared using DECLARE CURSOR statement.
- Transact-SQL Server cursors can be used in Transact-SQL scripts, stored procedures, and triggers.
- Transact-SQL cursors are implemented on the server.
- You can fetch only one row at a time in Transact-SQL Server cursors.
- You can use FETCH statements with Transact-SQL cursors to retrieve rows from a cursor’s result set.
- API server cursors support the API cursor functions.
- API server cursors are implemented on the server.
- API server cursors support fetching blocks of rows with each fetch.
- A cursor fetches multiple rows at a time is called a block cursor

What are cursors?

A cursor is used to access the result set stored in the memory on execution of a query. It is a special programming construct that allows data to be manipulated on a row-by-row basis. They point to a certain location within a record set and allow the operator to move forward (and sometimes backward, depending upon the cursor type) through the results one record at a time.
Define the steps to use Transact-SQL Cursor
Declare the cursor, Open the cursor, Fetch record row by row, Close cursor, Deallocate cursor........
Explain the cursor types
DYNAMIC: It reflects changes happened on the table while scrolling through the row........
Define the cursor lock types
Three types of locks READ ONLY: This prevents any updates on the table........
Post your comment