Generate a set or sequence without loops – part 3 – SQLPerformance.com

Since we’ve already established multiple ways to derive a series of numbers, let’s look at how the next step looks. Let’s start very simple, and pretend we want to run a report for three days, from January 1st through January 3rd, and include a row for every day. The old-fashioned way would be to create a #temp table, create a loop, have a variable that holds the current day, within the loop insert a row into the #temp table until the end of the range, and then use the #temp table to outer join to our source data. That’s more code than I even want to present here, never mind put in production, maintain, and have colleagues learn from.