0

我有一张合适房间的桌子

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
4

1 回答 1

0

我可以在不使用循环的情况下为 MS SQL 2000 建议替代解决方案:

SET NOCOUNT ON;

DECLARE @SearchInterval TINYINT
SELECT @SearchInterval = 30

DECLARE @SuitableRooms TABLE 
(
      RoomID BIGINT
    , RoomStartTime DATETIME
    , RoomEndTime DATETIME
)

INSERT INTO @SuitableRooms (RoomID, RoomStartTime, RoomEndTime) 
SELECT 1, '2013-02-26 08:00:00', '2013-02-26 17:00:00'
  UNION ALL
SELECT 2, '2013-02-26 10:00:00', '2013-02-26 19:00:00'
  UNION ALL
SELECT 3, '2013-02-26 09:00:00', '2013-02-26 17:00:00'

DECLARE @emun TABLE (i BIGINT IDENTITY, blank BIT)
INSERT INTO @emun (blank)
SELECT 1
FROM [master].dbo.spt_values n
CROSS JOIN (
    SELECT i = 1 
      UNION ALL 
    SELECT 2 
) b

SELECT r.RoomID, StartTime = DATEADD(MINUTE, i, RoomStartTime)
FROM (
    SELECT i = 0

    UNION ALL

    SELECT i
    FROM @emun
    WHERE i % @SearchInterval = 0
) d
CROSS JOIN @SuitableRooms r
WHERE DATEADD(MINUTE, i, RoomStartTime) < RoomEndTime
ORDER BY StartTime

对于 MS SQL 版本 > 2000(很遗憾 MS SQL 2000 不支持 CTE):

SET NOCOUNT ON;

DECLARE @SearchInterval TINYINT
SELECT @SearchInterval = 30

;WITH emun AS 
(
    SELECT  
          RoomID
        , StartTime
        , EndTime
    FROM (
        SELECT 
              RoomID = 1
            , StartTime = CAST('2013-02-26 08:00:00' AS DATETIME)
            , EndTime = '2013-02-26 17:00:00'

          UNION ALL

        SELECT 2, '2013-02-26 10:00:00', '2013-02-26 19:00:00'
          UNION ALL
        SELECT 3, '2013-02-26 09:00:00', '2013-02-26 17:00:00'
    ) d

    UNION ALL

    SELECT  
          RoomID
        , StartTime = DATEADD(MINUTE, @SearchInterval, StartTime)
        , EndTime
    FROM emun
    WHERE DATEADD(MINUTE, @SearchInterval, StartTime) < EndTime
)

SELECT RoomID, StartTime
FROM emun
ORDER BY StartTime
于 2013-04-04T08:31:59.360 回答