0

我正在尝试编写一个查询以根据以下内容选择记录:

  1. 它们应该属于特定类别
  2. 它们应该在开始日期和结束日期之间。

    我有这样的记录:

    EventId| EventName| Event_Category| Event_StartDate| Event_EndDate|
       1   |  aaa     |     4         |  2014-06-10    |  2014-06-15  |
       2   |  bbb     |     5         |  2014-06-10    |  2014-06-15  |
       3   |  ccc     |     6         |  2014-06-10    |  2014-07-11  |
       4   |  ddd     |     5         |  2014-06-01    |  2014-07-10  |
       5   |  eee     |     5         |  2014-06-10    |  2014-06-014 |
    

    存储过程:

    create proc usp_filterevnts @Categoryid int, @StartDate date, @EndDate date
    as
    begin 
    select * from Events Where Event_Category=@Categoryid  and ( Event_Startdate>@StartDate and Event_Enddate<@EndDate )
    end
    

    如果我执行 sp 作为

      exec usp_filterevnts 5,'2014-06-09','2014-06-16' 
    

    它应该显示第 5 类记录的记录,其开始日期和结束日期在 '2014-06-09' 和 '2014-06-16' 之间......即第 2 条记录,第 5 条记录..

    我的存储过程对此是否正确?

4

2 回答 2

0
select * from Events Where Event_Category=@Categoryid and (Event_Startdate between @StartDate and @EndDate) and (Event_Enddate between @StartDate and @EndDate)
于 2014-06-30T13:22:18.250 回答
-1

(取决于表格大小,以及日期字段是否有索引...)

您的原始语法,但包含 >= 和 <=

之间会导致表扫描,如果您的表很小,这不会太重要。

于 2014-06-30T15:12:34.627 回答