Oracle nvl 在 where 子句中显示奇怪的结果?
我有一个 Web 表单,允许用户根据传递到过程的参数搜索和编辑 Oracle 表中的记录。这是我的数据:
CAE_SEC_ID SEC_CODE APPR_STATUS
1 ABC1 100
2 ABC2 100
3 ABC3 101
4 (null) 101
5 (null) 102
6 ABC4 103
这是 where 子句:
select foo
from bar
where CAE_SEC_ID = NVL(p_cae_sec_id,CAE_SEC_ID)
and Upper(SEC_CODE) like '%' || Upper(NVL(p_sec_code,SEC_CODE)) || '%'
and APPR_STATUS = NVL(p_appr_status, APPR_STATUS)
如果任何参数具有值,则对参数使用 nvl 应该仅返回匹配的记录,如果没有参数具有值,则返回所有记录。一切都很标准,我是这么想的。但是,当我在没有任何参数值的情况下进行搜索时,查询不会返回具有 null SEC_CODE 的记录,即仅返回记录 1、2、3 和 6。上面的 where 子句不应该包含具有空 SEC_CODE 值的记录吗?
I have a web form that allows users to search on and edit records from an Oracle table based on parameters passed in to a proc. Here's my data:
CAE_SEC_ID SEC_CODE APPR_STATUS
1 ABC1 100
2 ABC2 100
3 ABC3 101
4 (null) 101
5 (null) 102
6 ABC4 103
And here's the where clause:
select foo
from bar
where CAE_SEC_ID = NVL(p_cae_sec_id,CAE_SEC_ID)
and Upper(SEC_CODE) like '%' || Upper(NVL(p_sec_code,SEC_CODE)) || '%'
and APPR_STATUS = NVL(p_appr_status, APPR_STATUS)
Using nvl on the parameters should return only the matched records if any of the parameters have values, and all records if none of the parameters have values. All pretty standard or so I thought. However when I do a search without any parameter values the query isn't returning records with a null SEC_CODE i.e. only records 1, 2, 3, and 6 are being returned. Shouldn't the where clause above include records with null SEC_CODE values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是表中的 SEC_CODE 值为 NULL。这意味着 UPPER(sec_code) 是 NULL 并且您的第二个谓词简化为
就像 NULL 不等于任何东西并且不等于任何东西一样,它不像任何东西。最有可能的是,您想要类似的内容
,如果
P_SEC_CODE
为 NULL,则返回每一行,但如果P_SEC_CODE
为非 NULL,则仍应用过滤器。The problem is that the
SEC_CODE
value in the table is NULL. That means thatUPPER(sec_code)
is NULL and your second predicate simplifies toJust like NULL is not equal to anything and not unequal to anything, it is not like anything. Most likely, you want something like
That will return every row if
P_SEC_CODE
is NULL but still apply the filter ifP_SEC_CODE
is non-NULL.不,不应该。
数据库中的 SEC_CODE 为空,因此 UPPER(SEC_CODE) 为空,因此在 LIKE 匹配或除 IS NULL 之外的几乎任何其他比较中都会失败。从技术上讲,它是未知的而不是错误的,但不足以通过测试。
No it shouldn't.
The SEC_CODE in the database is null, so the UPPER(SEC_CODE) is null and so it will fail on a LIKE match or pretty much any other comparison beyond IS NULL. Technically it is a UNKNOWN rather than a FALSE but is isn't enough to pass the test.
表达式
NULL = NULL
的计算结果为NULL
,它不是true
,因此不会返回这些行。如果我正确理解了要求,您只想过滤参数是否不同于 null,因此我会像这样重写查询以使过滤器更加明确:将 p_sec_code 参数设置为 null 现在将返回所有行,忽略该值在 SEC_CODE 列中。
The expression
NULL = NULL
evaluates toNULL
, which is nottrue
and so these rows won't be returned. If I understand the requirement correctly, you only want to filter if a parameter is different from null, so I would rewrite the query like this to make the filter more explicit:Setting the p_sec_code parameter to null will now return all rows, ignoring the value in the SEC_CODE column.
我们还可以编写 Jorn 的查询,使其
具体化并提高性能。
We can also write the Jorn's query like
to make it concrete and increase the performance .