Skip to main content
1 de 2

DECLARE @dataInicio DATE DECLARE @dataFim DATE

BEGIN CREATE TABLE #calendario ( data DATE, yearmonth VARCHAR(9) )

SET @dataInicio = ( '1900-01-01' ) 
SET @dataFim = ( Cast(Getdate() AS DATE) ) 

WHILE @dataInicio <= @dataFim 
  BEGIN 
      INSERT INTO #calendario 
      VALUES      (@dataInicio, 
                   (SELECT Format(@dataInicio, 'MM-yyyy'))) 

      SET @dataInicio = Dateadd(day, 1, @dataInicio) 
  END 

SELECT * 
FROM   #calendario 
ORDER  BY data DESC 

END