Sunday, February 19, 2012

For Each

Is it possible to do a foreach loop in T-SQL? I need to populate a database w/ dummy data for testing and need to be able to write a SP that will cycle through each record in one table and populate other table(s) based on what is in that record. Any ideas?there is no for each loop but you can certainly get the data into atemporary table or table variable and loop through each record. You canuse the primary key column to go to the next row.
|||You are thinking procedurally. This is not how SQL works. You need to think in sets.
What you are trying to do can likely be solved with a single UPDATE statement with a few CASEs.

No comments:

Post a Comment