简体   繁体   English

Oracle:NULL与EMPTY字符串之间的区别

[英]Oracle:Difference between NULL and EMPTY string

I am in a situation where my query is not returning any values due to oracle behaviour. 由于Oracle行为,我的查询未返回任何值。

Problem is this: Oracle considers EMPTY STRING as NULL when INSERTING DATA but not when SELECTING BACK the data . 问题是这样的: 甲骨文插入数据时,但不选择回数据时,考虑空字符串NULL

This is not a duplicate of Why does Oracle 9i treat an empty string as NULL? 这不是Oracle 9i为什么将空字符串视为NULL的重复项 because here i am not asking for the reason to this problem,i am well aware of the reason,i am asking for a solution to this problem. 因为在这里我不是要问这个问题的原因,我很清楚原因,我是在寻求解决这个问题的方法。

This is my table structure 这是我的表结构

   CREATE TABLE TEST
    (
      ID          NUMBER not null,
      NAME VARCHAR2(255)
    )

when inserting the values oracle will accept 插入值时,oracle将接受

INSERT INTO TEST values(1,''); 插入测试值(1,'');

I found out that internally oracle converts Strings of Zero length to NULL and stores 我发现内部oracle将零长度的字符串转换为NULL并存储

But my query 但是我的查询

SELECT * FROM TEST WHERE NAME = INPUT;(INPUT='') SELECT * FROM TEST WHERE NAME = INPUT;(INPUT ='')

(Input is passed from front end and will sometimes have empty string) (输入是从前端传递的,有时会有空字符串)
will not return any result 不会返回任何结果
I can not write dynamic query due to performance issue 由于性能问题,我无法编写动态查询

Somebody who faced this issue before please let me know how do i compare EMPTY STRING with NULL 之前曾遇到此问题的人请让我知道如何比较EMPTY STRING和NULL

The problem is that Oracle (by default) treats empty strings as NULL . 问题在于,Oracle(默认情况下)将空字符串视为NULL Hence: 因此:

where name = ''

is the same as: 是相同的:

where name = NULL

and both always fail (because they return NULL ). 两者都总是失败(因为它们返回NULL )。

You can fix this in various ways. 您可以通过多种方式解决此问题。 One method is: 一种方法是:

where (name = INPUT or name is null and INPUT is null)

Or, if you know there is an invalid name: 或者,如果您知道名称无效:

where coalesce(name, '<invalid>') = coalesce(INPUT, '<invalid>')

This is one of the most annoying features of Oracle - not found in other DB products. 这是Oracle最令人讨厌的功能之一-在其他数据库产品中找不到。 You will have to put up with it, for all the other massive advantages of Oracle - and be prepared that the learning curve is not very quick. 对于Oracle的所有其他其他优点,您将不得不忍受它-并且要准备好学习曲线不是很快。

To check for equality of nulls, the best approach is to write explicitly what you are doing, instead of using gimmicks. 要检查null是否相等,最好的方法是显式地编写您在做什么,而不是使用using头。 For example: 例如:

... where NAME = INPUT or (NAME IS NULL and INPUT IS NULL)

This will make it a lot easier for yourself, and for others after you, to debug, maintain, and modify the code, now and especially later. 这将使您自己以及以后的其他人现在(尤其是以后)调试,维护和修改代码变得容易得多。 There are other solutions, too, but they may confuse others in the future; 也有其他解决方案,但是将来它们可能会使其他人感到困惑。 for example, this is something I wouldn't use (for several reasons): 例如,这是我不会使用的(出于几个原因):

... where NAME || 'z' = INPUT || 'z'

although it would obviously achieve the same result with less typing. 尽管显然用更少的键入即可达到相同的结果。

One more thing, in most cases you should NOT include in your results rows where you treat NULL as "equal" - the values are NULL for a reason, and in most cases if you make two NULL's equal, that is NOT the intended result. 还有一件事,在大多数情况下,您不应该在将NULL视为“等于”的结果行中包括-值是NULL是有原因的,并且在大多数情况下,如果使两个NULL相等,则这不是预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM