--功能:使用递归算法生成一个序列 --日期:2019-11-30 --作者a:ligq --功能:1、生成等差数列 DECLARE @ib INT = 1; --第一项的值 DECLARE @ig INT = 2; --公差 DECLARE @imax INT = 100; --序列中的最大值 WITH ctea (i) AS (SELECT @ib AS i UNION ALL SELECT i + @ig --这里修改公差 FROM ctea WHERE i <= @imax - @ig) --注意这里最大值减去公差 SELECT * FROM ctea OPTION (MAXRECURSION 0); GO ; --功能:2、生成日期序列 DECLARE @dbegin DATE = '2019-1-1'; --起始日期 DECLARE @dend DATE = '2019-3-1'; --结束日期 WITH cteb (ddate) AS (SELECT @dbegin AS ddate UNION ALL SELECT DATEADD(DAY, 1, ddate) FROM cteb WHERE ddate <= DATEADD(DAY, -1, @dend)) SELECT * FROM cteb OPTION (MAXRECURSION 0);