你可以分三块做。第一段是第一个小时,60 减去分钟值,第二段是时间 = 60,开始 + 1 和结束之间的所有小时,第三段是结束分钟
然后将它们插入到临时表中,就像 abstractChaos 所做的那样。
像 AbstractChaos 一样插入临时表:
DECLARE @STARTDATE AS SMALLDATETIME
DECLARE @ENDDATE AS SMALLDATETIME
DECLARE @TIME AS TABLE(id INT IDENTITY(1,1), [HOUR] INT, [TIME] INT)
SET @STARTDATE = '2012-11-21 11:03:00'
SET @ENDDATE = '2012-11-21 13:04:00'
INSERT INTO @TIME (HOUR,TIME)
VALUES (datepart(HOUR,@startdate) ,60 - datepart(MINUTE,@startdate) )
WHILE @STARTDATE < @ENDDATE
BEGIN
SELECT @STARTDATE = DATEADD(MINUTE,60,@STARTDATE)
INSERT INTO @TIME (HOUR,TIME)
VALUES(datepart(HOUR,@STARTDATE) , 60)
END
INSERT INTO @TIME (HOUR,TIME)
VALUES(datepart(HOUR,@enddate) , datepart(MINUTE,@startdate))