SQL Server With 递归 日期 循环
要实现的效果:查询从Date From 到 To 之间的 所有日期;
示例代码如下:
DECLARE @DATE_FROM DATETIME = N'2016-05-16';--N'2015-05-16';
DECLARE @DATE_TO DATETIME = N'2016-05-17';
WITH tmpDays AS
(
SELECT @DATE_FROM DATE_VALUE
UNION ALL
SELECT DATE_VALUE + 1
FROM tmpDays
WHERE DATE_VALUE + 1 <= @DATE_TO
)
SELECT * FROM tmpDays OPTION (MAXRECURSION 0);
posted on 2016-05-17 10:10 freeliver54 阅读(787) 评论(0) 编辑 收藏 举报