存储过程不返回任何行,但相同的查询返回正确的数据

时间:2016-02-27 21:23:24

标签: sql sql-server visual-studio stored-procedures

这是我的存储过程:

lin : 101 (checksum: -1741910392)
lock: 89 (checksum: -1741910392)
circ: 102 (checksum: -1741910392)

执行时,它不会返回任何值,但是如果我执行此查询:

CREATE PROC [SPmainReport]
@startDate date = null,
@endDate date = null,
@customerName varchar = null,

AS
SELECT Distinct
    VI.Code as CustomerCode, VI.Name as CustomerName, VI.Area as CustomerArea, VI.[Address] as [address], CP.ProductName as ProductName, CP.ProductQuantity as Quantity
from 
VendorTrading VT inner join CustomerProducts CP on VT.Id = CP.VendorTradingId inner join VendorInfo VI on VT.VendorId = VI.Id
where
 (VT.Tradedate between isnull(@startDate,VT.Tradedate) and isnull(@endDate,VT.Tradedate))
 and VI.Name = ISNULL(@customerName, VI.Name)

它返回完全需要的数据。我很困惑为什么会发生这种情况。完全没有区别。我确保脚本在同一个数据库上运行,它还包含完美的数据。这是SP执行脚本:

    SELECT Distinct
    VI.Code as CustomerCode, VI.Name as CustomerName, VI.Area as CustomerArea, VI.[Address] as [address], CP.ProductName as ProductName, CP.ProductQuantity as Quantity
from 
VendorTrading VT inner join CustomerProducts CP on VT.Id = CP.VendorTradingId inner join VendorInfo VI on VT.VendorId = VI.Id
where
 (VT.Tradedate between isnull(@startDate,VT.Tradedate) and isnull(@endDate,VT.Tradedate))
 and VI.Name = ISNULL('John', VI.Name)

我注意到的另一个奇怪的事情是,如果我修改此SP并使用DATES限制条件,而不是名称。它工作正常。 我正在研究SQL Server的Visual Studio 2013界面。不是管理工作室(如果它确实重要)

1 个答案:

答案 0 :(得分:8)

在SQL Server中,始终在使用varchar()char()及相关类型时使用长度参数:

CREATE PROCEDURE SPmainReport (
    @startDate date = null,
    @endDate date = null,
    @customerName varchar(255) = null
)
BEGIN
    . . . 
END;

大多数客户名称可能包含多个字符。