背景:要自动生成一个日期表
DECLARE @year INT=year(getdate()); --DECLARE @year INT=year(:endTime); WITH t AS (SELECT CAST( CAST(@year AS VARCHAR(4)) + '-1-1' AS DATE) AS Dt UNION ALL SELECT DATEADD(DAY,1, t.Dt) FROM t WHERE t.Dt<CAST(@year AS VARCHAR(4)) + '-12-31') select * from t a OPTION(MAXRECURSION 366)