is the query that will determine the rows that make up the cursor.Note there are other parameters/options associated with the DECLARE CURSOR statement that help define more complicated cursor processing than I will be covering in this article.The @@FETCH_STATUS variable controls whether the WHILE loop is [email protected]@FETCH_STATUS is set to zero when a record is successfully retrieved from the cursor "Cust List".To define a cursor the DECLARE CURSOR statement is used.Here is the basic format for the simple cursor topic I will be discussing in this article.At some point you will have some business logic that will require you to process sequentially through a set of records one record at a time.
For the purpose of this article I'm only going to focus on how to use the default scrolling functionality of a cursor.
Prior to returning to the top of the WHILE loop I used another "SELECT TOP 1" statement to select the next Customer ID.
This SELECT statement uses a WHERE clause on the SELECT statement to select the first Customer ID that is greater than the Customer ID that was just printed.
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.