我有合適的房間的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