gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:42:06 25 4
gpt4 key购买 nike

这是我的存储过程:

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 界面。不是管理工作室(如果它确实重要的话)

最佳答案

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

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

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

关于sql - 存储过程不返回任何行,但相同的查询返回正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35675610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com