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