Steps to Use Cursors
As shown in Listing 12.8, to use a cursor you must follow this sequence:
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.
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.
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access