为什么null在plsql中的语句中是正确的
大家好,我想知道有人知道为什么这个PL/SQL代码会给结果错误,但是我在示出了结果会提高异常,但是我没有明白,为什么像NVL这样的NVL保护我们免受我所知的无效价值。
declare
i_send_type varchar2(100) := null;
begin
if nvl(i_send_type,'') <> 'cash' and nvl(i_send_type,'') <> 'card' then
raise_application_error(-20032,'o_userMessage');
end if;
end;
Hello guys I was wondering anyone knows why this pl/sql code give a result false but I was excpeting the result will be raise exception but I didn't get it why like that nvl protect us from null value as I know.
declare
i_send_type varchar2(100) := null;
begin
if nvl(i_send_type,'') <> 'cash' and nvl(i_send_type,'') <> 'card' then
raise_application_error(-20032,'o_userMessage');
end if;
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是因为
null
的空字符串 - oracle中的一个空字符串是等效的,这意味着nvl
实际上什么也没做;它说在这种情况下,您比较了
null
null 到'cash'
和'card'
使用&lt;&gt;
,但null
无法比较。方式 - 您必须使用是null
或不是null
。那么,该怎么办?
一个选项:不要在
nvl
中使用空字符串,但例如'x'
(或其他无法是i_send_type
的其他字符串:另一个(更好的)选项:检查 - 正如我所说 - 变量的值
是否为null
:That's because an empty string - in Oracle - is equivalent of
NULL
, which means thatNVL
actually did nothing; it saidIn that case, you compared
NULL
to'cash'
and'card'
using<>
, butNULL
can't be compared that way - you have to useIS NULL
orIS NOT NULL
.So, what to do?
One option: don't use empty string in
NVL
but e.g.'x'
(or some other string that can't be contents ofi_send_type
:Another (better) option: check - as I said - whether variable's value
IS NULL
:。它给出了结果不正确。
不正确的二进制逻辑中的false仅相同,而有两个可能的值是正确的或错误。在数据库(不仅是Oracle)中,它们使用三元逻辑,并且可以具有以下值:true,false或未知(
null
)。例如,如果您比较= 运算符:
然后
null =&lt; nothing&gt;
或&lt; nothing&gt; = null
始终给出结果null
(而不是false),因为您无法确定未知是否等于其他任何内容(甚至是另一个未知),因为值未知。对于
&lt;&gt;
运算符,逻辑与上述相似,true
andfalse
交换在输出中,但结果仍然是null
(而不是true
或false
)当两个输入为null
时。在Oracle中,空字符串
''
与null
sonvl(i_send_type,'')
与nvl( i_send_type,null)
与i_send_type
相同,因此您最终获得了比较null&lt;&gt;&gt; '现金'或null&lt;&gt; 'card'
导致null
(而不是true
或false
)。要解决它,您可以反转操作员并在中使用
=
或,然后使用
else
条件:或者,如果要骑行在
else
的情况下,您必须使用的
是null :null
值It does not. It gives a result as not true.
Not true is only the same as false in binary logic where there are two possible values, true or false. In databases (and not just Oracle), they use trinary logic and can have the values: true, false or unknown (
NULL
).For example, if you compare the
=
operator:Then
NULL = <anything>
or<anything> = NULL
always gives the resultNULL
(and not false) because you cannot tell if an unknown is equal to anything else (even another unknown) as the values are unknown.For the
<>
operator, the logic is similar to above withTRUE
andFALSE
swapped in the output but the result is stillNULL
(and notTRUE
orFALSE
) when either input isNULL
.In Oracle, the empty string
''
is identical toNULL
sonvl(i_send_type,'')
is the same asnvl(i_send_type,NULL)
which is the same asi_send_type
so you end up with the comparisonsNULL <> 'cash' OR NULL <> 'card'
which results inNULL
(and notTRUE
orFALSE
).To solve it, you can reverse the operator and use
=
orIN
and then use anELSE
condition:or, if you want to get rid of the
ELSE
then you have to explicitly check forNULL
values usingIS NULL
: