1 if OBJECT_ID('tempdb..#temptblAllYearMonth') is not null 2 drop table #temptblAllYearMonth 3 4 declare @StartDate DATE = '20210101' 5 declare @EndDate DATE = '20211201'; 6 7 WITH cte as ( 8 select @StartDate dateCol union all select DATEADD(MONTH, 1, dateCol) 9 from cte where dateCol < @EndDate 10 ) 11 select CONVERT(varchar(6), dateCol, 112) dateCol, 0 as ToTal 12 13 into #temptblAllYearMonth from cte; 14 15 --查询数据 16 select * from #temptblAllYearMonth