sql-server - 存储过程传递多个参数,条件结果未按预期工作
问题描述
我正在尝试创建这个存储过程,但它没有按预期工作,所以当我将参数传递给它时,我没有得到结果。
这是我的存储过程,它正在返回结果:
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)
有谁知道这是为什么?
解决方案
它的安全使用coalesce()
功能包括这些null
值。
and iif(@PaymentStatusId = 0, 1, coalesce([PaymentStatusId], 0))
= iif(@PaymentStatusId = 0, 1, @PaymentStatusId)
推荐阅读
- sql - SQL 存储用户信息的最佳实践?
- java - Gradle api传递依赖项不起作用
- flutter - 使用插件 cached_network_image 在 Flutter 中缓存持续时间?
- java - 命名参数未绑定
- javascript - 返回 TYPE BOOLEAN 的 VARIABLE NAME 而不是 Template Literal 中的 VALUE 本身的 JavaScript 方法
- templates - 在 keycloak 存储 SPI 中使用主题资源来扩展管理主题
- elasticsearch - Filebeat:如何从 kube-system 命名空间中排除日志
- scheme - car 和 cdr 的简写
- javascript - 如何对定义为字符串的变量执行 forEach 循环 | 大批
? - sql - 使用 BigQuery 查询 UUID