ISNULL 与 NULL 数据库字段的用法

发布于 2024-11-07 00:03:09 字数 326 浏览 4 评论 0原文

我正在尝试做一些应该相当简单的事情,但 ISNULL 没有做我想象的事情。

基本上我有一个存储过程,我希望 PARAM1 或 PARAM2 在我的表中具有匹配的值。

SELECT * FROM MyTable WITH (NOLOCK)
        WHERE      
        field1 = ISNULL(@PARAM1 ,field1 )
        AND        
        field2 = @PARAM2

这工作正常,直到我的行中有 NULL 字段,然后它排除这些结果。有没有一种不同的方法可以满足这个需求?

I am trying to do something that should be fairly simple but ISNULL isn't doing what I thought it would.

Basically I have a stored procedure and I am expecting either PARAM1 OR PARAM2 to have a matching value in my table.

SELECT * FROM MyTable WITH (NOLOCK)
        WHERE      
        field1 = ISNULL(@PARAM1 ,field1 )
        AND        
        field2 = @PARAM2

This works fine until I have NULL fields in my row then it excludes those results. Is there a different method that can cater for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上得阳光 2024-11-14 00:03:09

ISNULL 将第一个值替换为第二个值,因此仅当参数 @PARAM1 为 NULL 时,它才会将其替换为 PARAM1。我假设您没有传递 NULL 值,所以这可能不是您想要的。更有可能的是,您只想说

WHERE
(Field1 = @PARAM1 OR Field1 IS NULL)
AND
Field2 = @Param2

我假设您也可以以这种方式使用 ISNULL:

ISNULL(Field1, @PARAM1) = @PARAM1

ISNULL replaces the first value with the second value, so only if your parameter @PARAM1 is NULL does it replace it with PARAM1. I assume you're not passing in NULL values, so that's probably not what you want. More likely you just want to say

WHERE
(Field1 = @PARAM1 OR Field1 IS NULL)
AND
Field2 = @Param2

I Suppose you could use ISNULL in this fashion too:

ISNULL(Field1, @PARAM1) = @PARAM1
万水千山粽是情ミ 2024-11-14 00:03:09

null在sql中是比较特殊的。如果您对列进行任何比较,则该列具有空值的任何行都将被排除。

SELECT * FROM MyTable WITH (NOLOCK)
        WHERE      
        (PARAM1 = @PARAM1 or PARAM1 is null)
        AND        
        (PARAM2 = @PARAM2 or PARAM2 is null)

null is special in sql. If you do any comparisons on a column any rows that have a null for that column will be excluded.

SELECT * FROM MyTable WITH (NOLOCK)
        WHERE      
        (PARAM1 = @PARAM1 or PARAM1 is null)
        AND        
        (PARAM2 = @PARAM2 or PARAM2 is null)
梦在深巷 2024-11-14 00:03:09

使用

field1 = ISNULL(@PARAM1, A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL)

这将计算为

如果@PARAM1 IS NOT NULL,则

field1 = @PARAM1。 field1 = A_REPLACMENT_VALUE_IF_PARAM1_IS_NULL 如果@PARAM1 IS NULL

编辑:

尝试这些:

--If you want to ignore the WHERE clause if PARAM1/2 is null
ISNULL(field1, DEFAULT_VALUE) = ISNULL(@PARAM1, ISNULL(field1, DEFAULT_VALUE))
OR
ISNULL(field2, DEFAULT_VALUE) = ISNULL(@PARAM2, ISNULL(field2, DEFAULT_VALUE))

--To get all rows with field1/2 as PARAM1/2 and ignore everything else
field1 = @PARAM1 OR field2 = @PARAM2

Use

field1 = ISNULL(@PARAM1, A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL)

This will evaluate to-

field1 = @PARAM1 if @PARAM1 IS NOT NULL.

field1 = A_REPLACEMENT_VALUE_IF_PARAM1_IS_NULL if @PARAM1 IS NULL

EDIT:

Try these:

--If you want to ignore the WHERE clause if PARAM1/2 is null
ISNULL(field1, DEFAULT_VALUE) = ISNULL(@PARAM1, ISNULL(field1, DEFAULT_VALUE))
OR
ISNULL(field2, DEFAULT_VALUE) = ISNULL(@PARAM2, ISNULL(field2, DEFAULT_VALUE))

OR

--To get all rows with field1/2 as PARAM1/2 and ignore everything else
field1 = @PARAM1 OR field2 = @PARAM2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文