我有合适的房间的Transact SQL - 插入所有日期与指定的差距在几分钟
DECLARE @tblSuitableRooms TABLE
(
RoomID BIGINT PRIMARY KEY NOT NULL,
StartTime DATETIME NULL,
EndTime DATETIME NULL,
RoomStartTime DATETIME NULL,
RoomEndTime DATETIME NULL,
RoomStartTimeCaretaker DATETIME NULL,
RoomEndTimeCaretaker DATETIME NULL
)
表,需要插入行@tblPossiblyAvailable表
DECLARE @tblPossiblyAvailable TABLE
(
RoomID BIGINT NOT NULL,
StartTime DATETIME NOT NULL,
Processed BIT NOT NULL
)
与差距固定的分钟数(@AdvancedSearchInterval)。我已经在表中使用RoomStartTimes
INSERT INTO @tblPossiblyAvailable
SELECT sr.RoomID, sr.RoomStartTime, 0
FROM @tblSuitableRooms sr
WHERE sr.RoomStartTime IS NOT NULL
但现在我需要在一个特定的分钟数(AdvancedSearchInterval)的间隔与RoomStartTime和RoomEndTime之间的开始时间,以插入@tblPossiblyAvailable更多的记录。这将允许我检查一天的房间供应情况。
我需要的是这样的事情
RoomID开始时间
1 2013年2月26日09:00:00
1 2013年2月26日09:30:00
1 2013-02- 26 10:00:00
1 2013年2月26日10点30分零零秒
1 2013年2月26日11:00:00
2 2013年2月26日八时00分00秒
2 2013- 02-26 08:30:00
2 2013-02-26 09:00:00
2 2013-02-26 09:30:00
2 2013年2月26日10:00:00
3 2013年2月26日09:00:00
3 2013年2月26日09:30:00
我需要的东西就像一个循环说
insert into @tblPossiblyAvailable
select each room from @tblSuitableRooms
and take the start time for the room,
then take the start time + 30 minutes and insert that with the RoomID,
then take the last time inserted + 30 minutes and insert that with the RoomID
then take the last time inserted + 30 minutes and insert that with the RoomID
...
非常感谢您的任何帮助。
埃德
附:我正在使用SQL Server 2000
此解决方案似乎正常工作。任何人都可以想到一个更好的方法去实现它,我会非常感兴趣。
DECLARE @AdvancedSearchInterval tinyint
DECLARE @tblSuitableRooms TABLE
(
RoomID BIGINT PRIMARY KEY NOT NULL,
StartTime DATETIME NULL,
EndTime DATETIME NULL,
RoomStartTime DATETIME NULL,
RoomEndTime DATETIME NULL,
RoomStartTimeCaretaker DATETIME NULL,
RoomEndTimeCaretaker DATETIME NULL
)
DECLARE @tblPossiblyAvailable TABLE
(
RoomID BIGINT NOT NULL,
StartTime DATETIME NOT NULL,
Processed BIT NOT NULL
)
SET @AdvancedSearchInterval = 30
INSERT INTO @tblSuitableRooms
select 1, getdate(), getdate(), '2013-02-26 08:00:00', '2013-02-26 17:00:00', getdate(), getdate()
UNION ALL
select 2, getdate(), getdate(), '2013-02-26 10:00:00', '2013-02-26 19:00:00', getdate(), getdate()
UNION ALL
select 3, getdate(), getdate(), '2013-02-26 09:00:00', '2013-02-26 17:00:00', getdate(), getdate()
DECLARE @mins INT
SET @mins = 0
WHILE @mins < 1440
BEGIN
INSERT INTO @tblPossiblyAvailable
SELECT RoomID, DATEADD(MINUTE,@mins,RoomStartTime), 0
FROM @tblSuitableRooms
WHERE DATEADD(MINUTE,@mins,RoomStartTime) < RoomEndTime
SET @mins = @mins + @AdvancedSearchInterval
END
SELECT *
FROM @tblPossiblyAvailable
ORDER BY StartTime