The Cursor Operator appears in an execution plan when a query is executed within a cursor. Cursors are used for row-by-row processing of data, which can be useful for complex row-level operations that aren’t easy to achieve with standard SQL statements. However, cursors generally have performance drawbacks because SQL Server is optimized for set-based operations, not row-by-row processing.
Open cursor
Fetch cursor
For the demo we are creating a
table and inserting some records.
CREATE TABLE EmployeeSales ( EmployeeID INT, SaleAmount DECIMAL(10, 2), SaleDate DATE ); INSERT INTO EmployeeSales (EmployeeID, SaleAmount, SaleDate) VALUES (1, 100.00, '2022-01-01'), (2, 200.50, '2022-01-02'), (1, 150.00, '2022-01-03'), (3, 300.00, '2022-01-04'), (2, 250.00, '2022-01-05'); |
Now writing cursor to fetch the
data
-- Declare variables for cursor processing |
Cursor is expensive so we need to
avoid cursor.