title: better way to generate months/year table
body: <p>I am starting to use more tsql again. Is there a better way then the one below to generate a table containing the month and year integers given a start and end date?</p>
  
  <pre><code>DECLARE @FromDate DATETIME, @ToDate DATETIME
  SET @FromDate = '2012-01-01 00:00:00.000'
  SET @ToDate = '2012-31-12 23:59:59.000'
  
  DECLARE @MonthsAndYears table (Month INT, Year int)
  
  ;WITH dates AS 
  (
      SELECT @FromDate 'date'
          UNION ALL
      SELECT DATEADD(dd, 1, t.date) 
          FROM dates t
      WHERE DATEADD(dd, 1, t.date) &lt;= @ToDate
  )
  INSERT INTO @MonthsAndYears
  SELECT 
      DATEPART(MONTH, date),
      DATEPART(YEAR, date)
  FROM dates 
  GROUP BY 
  DATEPART(MONTH, date),
  DATEPART(YEAR, date)
  option (maxrecursion 0)
  </code></pre>