Sql procedure loop while updating records using cursors Club du sexe chatter
A WHILE loop is a programming construct that you're likely familiar with from other programming languages.
You define a condition at the beginning of the loop, and iteration will occur so long as this condition remains to be true.
The following snippet performs the same operation as the cursor above but through the use of a WHILE loop.
Notice that it is necessary to perform a query on the base table for each record returned.
(Note: These examples do no real processing.) I take the idcol field from the record and print it to the screen for each record in the table — simple stuff. As noted in the section on cursor types above, this is a combination of a READ_ONLY and a FORWARD_ONLY cursor.
Below is a list of the available cursor types as listed on Microsoft's SQL Server Books Online.
WHILE loops don't provide some of the bells and whistles that come with cursors, such as the ability to easily go "backward" in the result set.
Although, I've never been in a situation in which I did anything other than a straight loop through a result set with a cursor.
Notice the fld3 field in the table declaration; I'm making this a larger field so that fewer rows are created on a page of data.
In my opinion, this will make the test a little more realistic, as it will "simulate" a table that contains a variety of character columns along with some additional fields of data.
Search for sql procedure loop while updating records using cursors:
It may not be the best way to work with a set of data, but if you need to loop row by agonizing row (RBAR) in a T-SQL script then a cursor is one way of doing it.