帮助撰写大厅预订日期可用性搜索查询

时间:2009-10-21 10:56:49

标签: sql sql-server tsql

我需要帮助编写查询以查找大厅预订系统的时段可用性。详情如下

我有一个存放大厅细节的霍尔表和HallBooking表,它存储了预订的开始和时间..

霍尔 - HallId - 姓名

HallBooking - HallBookingId - HallId - BookingPersonName - StartDateTime - EndDateTime

用户可以通过指定开始日期时间和结束日期时间来搜索大厅可用性。查询应该选择并显示该大厅是否可用。

如果时间段不可用,则查询应选择当天具有相同持续时间的可用插槽并向用户显示。

提前致谢 ANZ

1 个答案:

答案 0 :(得分:1)

这是一个可以帮助您入门的测试脚本。

DECLARE @Hall TABLE
    (
      HallID INT PRIMARY KEY ,
      HallName VARCHAR(1)
    )
DECLARE @HallBooking TABLE
    (
      HallBookingID INT PRIMARY KEY ,
      HallID INT ,
      BookingPersonName VARCHAR(10) ,
      StartDateTime DATETIME ,
      EndDateTime DATETIME
    )


INSERT  @Hall
        SELECT  1 ,
                'A'
        UNION ALL
        SELECT  2 ,
                'B'
        UNION ALL
        SELECT  3 ,
                'C'


--TEST 1 Check for available Hall
DECLARE @HallID INT ,
    @StartDateTime DATETIME ,
    @EndDateTime DATETIME
DECLARE @Status VARCHAR(6)

SELECT  @HallID = 1 ,
        @StartDateTime = '20091021 8:00:00' ,
        @EndDateTime = '20091021 12:00:00'


--INSERT TestReservationCode here

SELECT  @Status = 'Open'
FROM    @Hall h
WHERE   h.HallID = @HallID
        AND NOT EXISTS ( SELECT *
                         FROM   @HallBooking hb
                         WHERE  h.HallID = hb.HallID
                                AND ( @StartDateTime >= hb.StartDateTime
                                      AND @StartDateTime < hb.EndDateTime
                                      OR @EndDateTime > hb.StartDateTime
                                      AND @EndDateTime <= hb.EndDateTime
                                    ) )


IF @Status IS NULL 
    BEGIN

        SELECT  @Status = 'Closed'

        SELECT  *
        FROM    @Hall h
        WHERE   h.HallID <> @HallID
                AND NOT EXISTS ( SELECT *
                                 FROM   @HallBooking hb
                                 WHERE  h.HallID = hb.HallID
                                        AND ( @StartDateTime >= hb.StartDateTime
                                              AND @StartDateTime < hb.EndDateTime
                                              OR @EndDateTime > hb.StartDateTime
                                              AND @EndDateTime <= hb.EndDateTime
                                            ) )


    END


SELECT  @Status

要测试预订状态,请在上面的INSERT注释下插入以下代码并重新运行。

--TestReservationCode
INSERT  INTO @HallBooking
        ( HallBookingID ,
          HallID ,
          BookingPersonName ,
          StartDateTime ,
          EndDateTime
        )
VALUES  ( 1 , -- HallBookingID - int
          1 , -- HallID - int
          'Stuart' , -- BookingPersonName - varchar(10)
          '2009-10-21 10:00' , -- StartDateTime - datetime
          '2009-10-21 12:00'  -- EndDateTime - datetime
        )

INSERT  INTO @HallBooking
        ( HallBookingID ,
          HallID ,
          BookingPersonName ,
          StartDateTime ,
          EndDateTime
        )
VALUES  ( 2 , -- HallBookingID - int
          2 , -- HallID - int
          'Stuart' , -- BookingPersonName - varchar(10)
          '2009-10-21 10:00' , -- StartDateTime - datetime
          '2009-10-21 12:00'  -- EndDateTime - datetime
        )