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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.