我有一张合适房间的桌子
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
但现在我需要在@tblPossiblyAvailable 中插入更多记录,其中 StartTime 介于 RoomStartTime 和 RoomEndTime 之间,间隔为一定的分钟数(AdvancedSearchInterval)。这将允许我检查一天的房间可用性。
我需要的是这样的
RoomID 开始时间
1 2013-02-26 09:00:00
1 2013-02-26 09:30:00
1 2013-02-26 10:00:00
1 2013-02-26 10:30:00
1 2013-02 -26 11:00:00
2 2013-02-26 08: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-02-26 10:00:00
3 2013-02-26 09:00:00
3 2013-02-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
...
非常感谢您的帮助。
埃德
PS 我使用的是 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