I found this nice article on using cursors to loop through a result set in a stored proc and take actions based on the value in the looped row:
This method will loop through all rows of a table regardless of what type the primary key is.
use YourDatabaseNameHere
godeclare @Field1 int
declare @Field2 int
declare @Field3 intdeclare MyCursor cursor fast_forward for
select Field1, Field2, Field3 from YourTableNameHereopen MyCursor
fetch next from MyCursor
into @Field1, @Field2, @Field3while @@fetch_status = 0
begin
— Perform Operations
declare myTest int
myTest = @Field1 + @Field2 + @Field3
– Advance the Cursor
fetch next from MyCursor
into @Field1, @Field2, @Field3
endclose MyCursor
deallocate MyCursor
go
This was on the following site: http://www.bunkerhollow.com/blogs/matt/archive/2009/03/10/ms-sql-loop-through-table-scripts.aspx




April 21st, 2010 at 7:30 pm
Согласен, это забавная фраза…
This method will loop through all rows of a table regardless of what type the primary key is…..
May 1st, 2010 at 8:32 am
УРА.!…
Всех с 1 мая!…