Using Cursors

Another way to return information to the user is to use cursors. A cursor is an object you can create in the database that is used to store multiple rows of data. This object can be looped through on a row-by-row basis, and operations can be performed against each row. In basic terms, cursors are similar to arrays in any other programming language.

Creating Cursors in SQL Server

There are five steps to working with cursors in SQL Server:

Cursor Step Explanation
DECLARE the cursor This process tells SQL Server that you are going to create a specific type of cursor. During the declaration, you specify a name for the cursor and SELECT statement, which will make up the rows through which the cursor loops.
OPEN the cursor ...

Get Writing Stored Procedures for Microsoft SQL Server now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.