【问题标题】:query all the earlier dates before a parameter sql server查询参数sql server之前的所有较早日期
【发布时间】:2014-09-21 19:53:15
【问题描述】:

下面的过程只显示我指定为参数的日期,而不是更早的日期。 我指定了@date <= table2.date_column;,但这句话只返回与日期匹配的值,而不是更早的日期

create procedure pro
(
    @code int, @date datetime, @total smallint OUTPUT
)
as
begin
select
    table1.column1,
    table2.date_column
from
    table1
    inner join
    table2 on table1.column1 = table2.column2
where
    table1.column1 = @code
    and
    table2.date_column = @date
    and
    @date <= table2.date_column;

set @total = @@rowcount;
end

执行

declare @total smallint 
exec pro '1', '20140920', @total=@total output
select @total

【问题讨论】:

  • 你还有table2.date_column = @date。拿出来。

标签: sql sql-server datetime stored-procedures parameters


【解决方案1】:

这是您的where 子句:

where table1.column1 = @code and
      table2.date_column = @date and
      @date <= table2.date_column;

您有两个与date_column 的比较。你只需要一个。为了平等:

where table1.column1 = @code and
      table2.date_column = @date;

对于之前的所有日期:

where table1.column1 = @code and
      table2.date_column <= @date;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-05
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多