文章目录
- Sqlserver递归生成日期范围
- 业务背景
- SQL脚本
- 执行结果
Sqlserver递归生成日期范围
业务背景
有时候需要按天生成数据,需要用到日期表进行循环处理
SQL脚本
;WITH DateRange AS
(
SELECT 1 AS Seq, '2024-01-01' AS DateValue
UNION ALL
SELECT Seq+1 Seq, CONVERT(VARCHAR(10),DATEADD(Day,1,DateValue) ,23) AS DateValue
FROM DateRange
WHERE DateValue<='2024-07-19'
)
--INSERT INTO #date_tables
SELECT * FROM DateRange OPTION(MAXRECURSION 0)