O'Reilly logo

Writing Stored Procedures for Microsoft SQL Server by Matthew Shepker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required