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

Aff*_*hab 6 sql sql-server stored-procedures visual-studio

这是我的存储过程:

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)
Run Code Online (Sandbox Code Playgroud)

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

    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)
Run Code Online (Sandbox Code Playgroud)

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

USE [E:\SANDWICH3\ABC\BIN\DEBUG\DATABASE\ABC.MDF]
GO

DECLARE @return_value Int

EXEC    @return_value = [dbo].[SPmainReport]
    @startDate = '2015-12-25',
    @endDate = '2015-12-25',
    @customerName = N'John'

SELECT  @return_value as 'Return Value'

GO
Run Code Online (Sandbox Code Playgroud)

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

Gor*_*off 8

在SQL Server中,总是使用时,使用长度参数varchar()char()与相关类型:

CREATE PROCEDURE SPmainReport (
    @startDate date = null,
    @endDate date = null,
    @customerName varchar(255) = null
)
BEGIN
    . . . 
END;
Run Code Online (Sandbox Code Playgroud)

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