Sử dụng CTE đệ quy
declare @StartTime datetime = '2011-07-20 11:00:33',
@EndTime datetime = '2011-07-20 15:37:34',
@Interval int = 554 -- this can be changed.
;WITH cSequence AS
(
SELECT
@StartTime AS StartRange,
DATEADD(SECOND, @Interval, @StartTime) AS EndRange
UNION ALL
SELECT
EndRange,
DATEADD(SECOND, @Interval, EndRange)
FROM cSequence
WHERE DATEADD(SECOND, @Interval, EndRange) < @EndTime
)
/* insert into tmp_IRange */
SELECT * FROM cSequence OPTION (MAXRECURSION 0);