SQL 报表 生成月份临时表

 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

posted @ 2021-08-17 09:48  Allen_666  阅读(244)  评论(0编辑  收藏  举报