SQL 服务器如果位参数 = 0 return 字段值为 0 或 null 的记录
SQL Server if bit parameter = 0 return records where field has value of 0 or null
我有一个大型存储过程(500 多行),从中 return 编辑了 Carrier 记录。我在调用此存储过程时向其传递值的参数之一是在 where 子句中用于过滤结果集。这是一个位类型参数,在我的实体模型(Web API)中用布尔值属性表示。
我不知道如何构造 where 子句以 return 结果集中的适当记录。
这是我想要发生的事情的基本轮廓:
- 如果位类型参数为
NULL
return全部记录
- 如果位类型参数等于
1
return指定位类型字段值为1
的所有记录
- 如果位类型参数等于
0
return 指定位类型字段为空或值为 0
的所有记录
我的尝试,断章取义(请原谅,我不是一个强大的 SQL 开发者):
DECLARE @Intrastate bit;
SET @Intrastate = 0; --should return all records where Intrastate field
--is null or has a value of 0
SELECT * FROM Carriers c
WHERE @Intrastate IS NULL
OR c.Intrastate = CASE WHEN @Intrastate = 1 THEN @Intrastate END
OR c.Intrastate = CASE WHEN @Intrastate = 0 THEN @Intrastate END
OR c.Intrastate = NULLIF(@Intrastate, 0)
以上查询仅 returns Intrastate 字段值为 0 的记录,不包括 Intrastate 字段为 null 的记录。
DECLARE @Intrastate bit;
SET @Intrastate = 0; --should return all records where Intrastate field
--is null or has a value of 0
SELECT * FROM Carriers c
WHERE @Intrastate IS NULL
OR c.Intrastate = CASE WHEN @Intrastate = 1 THEN @Intrastate END
OR c.Intrastate in (SELECT DISTINCT Intrastate FROM Carriers
WHERE Intrastate = 0
OR Intrastate IS NULL)
同样,上述查询仅 returns Intrastate 字段值为 0 的记录,不包括 Intrastate 为空的记录。
将 NULL
或 1
分配给 @Intrastate
参数会生成所需的结果集,如上文所定义。
当 @Intrastate
参数的值为 0
.
时,我的问题仅与结果集 return 有关
非常感谢任何帮助。
那……
...
WHERE @Intrastate IS NULL
OR ISNULL(c.Intrastate, 0) = @Intrastate
这样,如果变量为空,您将获得所有记录,否则,如果为空,则将列合并为 0,这应该涵盖 1=1 和 0=0/null=0。
如果您设置ANSI_NULLS关闭
,这将有效
SET ANSI_NULLS Off
GO
WHERE Intrastate in (isnull(@Intrastate,Intrastate), nullif(isnull(@Intrastate,0), 0))
好的,这个解决方案看起来需要大量输入,但如果 WHERE 子句中有 function on the predicate column,sql 服务器很可能会扫描整个 table并且不会使用您的索引:
SELECT * FROM Carriers c
WHERE @Intrastate IS NULL
OR (@Intrastate = 1 AND c.Intrastate = 1)
OR ( @Intrastate = 0 AND ( c.Intrastate IS NULL
OR c.Intrastate = 0 ))
我有一个大型存储过程(500 多行),从中 return 编辑了 Carrier 记录。我在调用此存储过程时向其传递值的参数之一是在 where 子句中用于过滤结果集。这是一个位类型参数,在我的实体模型(Web API)中用布尔值属性表示。
我不知道如何构造 where 子句以 return 结果集中的适当记录。
这是我想要发生的事情的基本轮廓:
- 如果位类型参数为
NULL
return全部记录 - 如果位类型参数等于
1
return指定位类型字段值为1
的所有记录
- 如果位类型参数等于
0
return 指定位类型字段为空或值为0
的所有记录
我的尝试,断章取义(请原谅,我不是一个强大的 SQL 开发者):
DECLARE @Intrastate bit;
SET @Intrastate = 0; --should return all records where Intrastate field
--is null or has a value of 0
SELECT * FROM Carriers c
WHERE @Intrastate IS NULL
OR c.Intrastate = CASE WHEN @Intrastate = 1 THEN @Intrastate END
OR c.Intrastate = CASE WHEN @Intrastate = 0 THEN @Intrastate END
OR c.Intrastate = NULLIF(@Intrastate, 0)
以上查询仅 returns Intrastate 字段值为 0 的记录,不包括 Intrastate 字段为 null 的记录。
DECLARE @Intrastate bit;
SET @Intrastate = 0; --should return all records where Intrastate field
--is null or has a value of 0
SELECT * FROM Carriers c
WHERE @Intrastate IS NULL
OR c.Intrastate = CASE WHEN @Intrastate = 1 THEN @Intrastate END
OR c.Intrastate in (SELECT DISTINCT Intrastate FROM Carriers
WHERE Intrastate = 0
OR Intrastate IS NULL)
同样,上述查询仅 returns Intrastate 字段值为 0 的记录,不包括 Intrastate 为空的记录。
将 NULL
或 1
分配给 @Intrastate
参数会生成所需的结果集,如上文所定义。
当 @Intrastate
参数的值为 0
.
非常感谢任何帮助。
那……
...
WHERE @Intrastate IS NULL
OR ISNULL(c.Intrastate, 0) = @Intrastate
这样,如果变量为空,您将获得所有记录,否则,如果为空,则将列合并为 0,这应该涵盖 1=1 和 0=0/null=0。
如果您设置ANSI_NULLS关闭
,这将有效SET ANSI_NULLS Off
GO
WHERE Intrastate in (isnull(@Intrastate,Intrastate), nullif(isnull(@Intrastate,0), 0))
好的,这个解决方案看起来需要大量输入,但如果 WHERE 子句中有 function on the predicate column,sql 服务器很可能会扫描整个 table并且不会使用您的索引:
SELECT * FROM Carriers c
WHERE @Intrastate IS NULL
OR (@Intrastate = 1 AND c.Intrastate = 1)
OR ( @Intrastate = 0 AND ( c.Intrastate IS NULL
OR c.Intrastate = 0 ))