You can also use a SELECT statement to process through a set of records one record at a time.To do this I will issue an initial SELECT statement that will return the first row, then a series of follow on SELECT statements where each SELECT statement retrieves the next row.In this code I will use two different "SELECT TOP 1" statements and a WHILE loop to return all 5 records. Here you can see the first SELECT statement selects only the first Customer ID. The WHILE loop is controled by the local variable @Row Num.Each time through the WHILE loop, the Row Number and Customer ID are printed out.This default functionality will only read from the first row to the last row in a cursor, one row at a time.I will leave additional cursor topics to another article series.The first method I will discuss uses a cursor to process through a set of records one record at a time.
This process continues one record at a time until all records in cursor "Cust List" have been processed.
You will need to decide which solution makes the most sense in your environment.
basis -- and I want to add my name to the list of people who wish cursors had never been introduced. Problems with cursors include extending locks, their inability to cache execution plans and CPU/RAM overhead.
The WHILE loop is process 5 times, allowing the SELECT TOP 1 method to retrieve the top 5 Customer ID's one records at a time.
This example produces the same printed output as my prior CURSOR example.