O'Reilly logo

Microsoft® SQL Server™ 2000 DBA Survival Guide, Second Edition by Mark Spenik, Orryn Sledge

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required