Putting It All Together
Now that you know something about cursor statements, positional updates, and global variables, the following cursor examples show you how all these components fit together.
Example 1: Loop Through a Table
The following example shows how the different components of a cursor (DECLARE, OPEN, FETCH, and DEALLOCATE) are used to loop through the publishers table. The @@fetch_status global variable is referenced each time a FETCH is performed. After the record pointer reaches the end of the resultset, the @@fetch_status variable is equal to -1. This prevents the code inside the while @@fetch_status = 0 section from being executed.
/* suppress counts from being displayed */ SET NOCOUNT ON /* declare a cursor that will contain ...