• Think about how you can build select statements to return the data in sets instead of using nested loops. The goal is to do operations on columns of data and not rows of data.

    Take a look at the article below by Jeff Moden on using a tally table. This might give you some ideas.

    http://www.sqlservercentral.com/articles/T-SQL/62867/