Steps to Use Cursors

As shown in Listing 12.8, to use a cursor you must follow this sequence:

  1. Use the DECLARE statement to declare the cursor. This step specifies the type of cursor and the query that defines the data to retrieve. SQL Server creates the memory structures that support the cursor. No data is retrieved yet.

  2. Execute the OPEN statement to open the cursor. In this step, SQL Server executes the query specified in the cursor definition and prepares the data for further navigation.

  3. Execute the FETCH statement to search for rows. In this step, you move the cursor pointer to the required row and, optionally, retrieve column values into variables. Repeat this step as many times as necessary to complete the required task. Optionally, you can ...

Get Microsoft® SQL Server™ 2000 Programming by Example 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.