Creating a Cursor

Every cursor must have at least four components. The four key components must follow this order:

1.
DECLARE the cursor
2.
OPEN the cursor
3.
FETCH from the cursor
4.
CLOSE or DEALLOCATE the cursor

Step 1: DECLARE the Cursor

The DECLARE statement contains the user-defined name used to reference the resultset, as well as the SQL SELECT statement that generates the resultset. Think of the DECLARE statement as a temporary table that contains a pointer to your actual data source.

DECLARE Cursor Syntax

The following is the SQL-92 cursor syntax:

DECLARE cursor_name [INSENSITIVE] [SCROLL] CURSOR 
FOR select_statement
[FOR {READ ONLY | UPDATE [OF column_list]}]

The following is the cursor extension syntax. (Note: The syntax that ...

Get Microsoft® SQL Server™ 2000 DBA Survival Guide, Second Edition 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.