这里是我的存储过程:存储过程不返回任何行,但相同的查询返回正确的数据
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)
在执行时不返回任何值,但如果我执行这个查询:
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执行脚本:
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
一个更奇怪的我已经注意到的是,如果我修改该SP和限制条件,只有日期,而不是名字。它工作得很好。 我正在开发SQL Server的Visual Studio 2013界面。不管理工作室使用varchar()
和char()
和相关类型时(如果这非常重要)
http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx(始终将长度分配给变量类型)&http ://blogs.sqlsentry.com/aaronbertrand/bad-habits-attachdbfilename/(为什么数据库名为'E:\ SANDWICH3 \ ABC \ BIN \ DEBUG \ DATABASE \ ABC.MDF'而不是'ABC'?) –
名称仍然是ABC,其余为目标路径。正如我所提到的,我使用Visual Studio界面 –