SQL 报表 生成月份临时表

时间:2021-08-17 10:23:32   收藏:0   阅读:15
 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

技术分享图片

原文:https://www.cnblogs.com/allen-zqw/p/15150993.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!