SQL Server存储过程传递了多个参数,但条件结果未按预期运行

问题描述 投票:0回答:1

我正在尝试创建此存储过程,但未按预期方式工作,因此,当我向其中传递参数时,不会得到结果。

这是我的存储过程,正在返回结果:

BEGIN
    DECLARE @sql NVARCHAR(MAX)

    SET NOCOUNT ON;

    SELECT TOP 100 *
    FROM [Test].[dbo].[Order] WITH (NOLOCK)
    WHERE [Deleted] = 0
      AND ([Id] = @OrderId OR @OrderId = 0)
      AND ([StoreId] = @StoreId OR @StoreId = 0)
      AND ([WarehouseId] = @WarehouseId OR @WarehouseId = 0)
      // if I insert payment status and statement here no results are returned
      AND [CreatedOnUtc] >= ISNULL(@CreatedFromUtc, '1/1/1900')
      AND [CreatedOnUtc] < ISNULL(@CreatedToUtc, '1/1/2999')
END

如果我将其添加到Warehouseid之后并声明它将停止返回结果:

AND ([PaymentStatusId] = @PaymentStatusId OR @PaymentStatusId = 0)

有人知道这是为什么吗?

sql-server stored-procedures conditional-statements
1个回答
0
投票

其安全使用的coalesce()功能包括这些null值。

  and iif(@PaymentStatusId = 0, 1, coalesce([PaymentStatusId], 0)) 
    = iif(@PaymentStatusId = 0, 1, @PaymentStatusId)
© www.soinside.com 2019 - 2024. All rights reserved.