Using Cursors to Change or Remove Data

Some applications need to work a row at a time with the results of a query. But relational databases like SQL Server are "set oriented," not "record oriented." Cursors can bridge this apparent mismatch. Applications can retrieve rows one at a time by using a cursor. A cursor is essentially a defined result set within which applications manipulate data row by row.

In a cursor, you can update or delete the current row by using an UPDATE or DELETE statement that includes a WHERE CURRENT OF clause. Changes made with this clause affect only the single row on which the cursor is positioned. In the case of a cursor based on a join, only the table_name specified in the DELETE or UPDATE statement is modified. Other tables participating in the cursor are not affected. For details on the UPDATE and DELETE statements, see the UPDATE and DELETE statements in the Microsoft SQL Server Transact-SQL Reference. For information about joins, see Retrieving Data with Queries.

For more information about cursors, see the Cursors topic in the Microsoft SQL Server Transact-SQL Reference. For details about cursors and locking, see Optimizing Performance.