Article ID: 111401 - Last Review: December 20, 2005 - Revision: 5.3 How to iterate through a result set by using Transact-SQL in SQL Server
This article was previously published under Q111401 On This PageSUMMARY
This article describes various methods that you can use to simulate a cursor-like FETCH-NEXT logic in a stored procedure, trigger, or Transact-SQL batch.
Use Transact-SQL Statements to Iterate Through a Result SetThere are three methods you can use to iterate through a result set by using Transact-SQL statements.One method is the use of temp tables. With this method, you create a "snapshot" of the initial SELECT statement and use it as a basis for "cursoring." For example: A second method is to use the min function to "walk" a table one row at a time. This method catches new rows that were added after the stored procedure begins execution, provided that the new row has a unique identifier greater than the current row that is being processed in the query. For example: APPLIES TO
| Other Resources Other Support Sites
CommunityGet Help NowArticle Translations
|





















Back to the top