查询参数sql server之前的所有早期日期

时间:2014-09-21 19:53:15

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

以下程序仅在我指定为参数的日期时显示,而不是在较早的日期。 我已指定@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

1 个答案:

答案 0 :(得分:0)

这是您的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;