存储过程不返回所有行

时间:2013-10-03 15:33:16

标签: sql sql-server stored-procedures

这可能是基本的,但我对SQL很新。我有一个存储过程,返回2个日期之间的记录。存储过程代码:

    CREATE PROCEDURE [dbo].[getSuggestionsByName] 

    @fromDate nvarchar(50),
    @toDate nvarchar(50)
AS
BEGIN
    SELECT name, unit, dept, ref, shortDescription, dateRaised, dateCompleted, suggestionType, awardType, awardAmount, dateCollected
    FROM suggestion.dbo.suggestions
    where dateRaised BETWEEN @fromDate and @toDate
END

GO

我正在使用以下命令进行测试:

exec getSuggestionsByName @fromdate = '1/09/2013', @todate = '3/10/2013'

这只返回3行,如下所示:

enter image description here

然而,当我在dbo.suggestions表上执行'select top 1000 rows'时,我会得到更多符合以下条件的行:

enter image description here

我不确定为什么这些记录不会被程序

返回

1 个答案:

答案 0 :(得分:2)

试试这个

SELECT name, unit, dept, ref, shortDescription, dateRaised, dateCompleted, suggestionType, awardType, awardAmount, dateCollected
FROM suggestion.dbo.suggestions
where convert(datetime,dateRaised,103) BETWEEN convert(datetime,@fromDate,103) and convert(datetime,@toDate,103)

这是因为你的column(dateRaised)parameters(@fromDate , @toDate)都是varchar,当你在2 varchar中有条件时,你会得到你所看到的结果。 当你将'1'和'2'定义为varchar时,'11'介于'1'和'2'之间