加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle – 检查varchar2是否为空的正确方法是什么?

发布时间:2020-12-12 13:09:34 所属栏目:百科 来源:网络整理
导读:According to official 11g docs Oracle Database currently treats a character value with a length of zero as null. However,this may not continue to be true in future releases,and Oracle recommends that you do not treat empty strings the same
According to official 11g docs

Oracle Database currently treats a character value with a length of
zero as null. However,this may not continue to be true in future
releases,and Oracle recommends that you do not treat empty strings
the same as nulls.

考虑一个函数getVersion,它返回一个可能是”的varchar2:

l_version := x.getVersion;
if l_version is null then
  return 'V.1.0';
end if;

这将在当前的Oracle 11g上正常运行,但是一旦未来的Oracle版本处理’与null不同,它可能会中断.

我认为做以上未来证明的唯一方法是:

如果l_version为null或l_version =”那么

有一种不那么繁琐的方式吗?

假设您在整个代码中使用varchar2,则l_version为null将是未来证明.

当ANSI标准声明varchar应该将NULL和空字符串视为单独的实体时,Oracle创建了varchar2数据类型.目的是varchar2数据类型的行为将继续保持一致,而未来的varchar可以使用新的标准NULL比较语义.当然,今天varchar和varchar2是彼此的同义词,并且它们已经存在了至少几十年,因此Oracle在未来实际改变varchar数据类型行为的几率非常低.

当您查看documentation for the VARCHAR2 and VARCHAR data types时,它讨论了VARCHAR的比较语义,可能在将来发生变化.不幸的是,他们所讨论的比较语义并不明确是NULL和空字符串之间的等价(或缺乏).但由于VARCHAR是ANSI标准数据类型,因此Oracle与ANSI标准之间的VARCHAR比较语义的唯一区别在于空字符串是否为NULL,这是普遍接受的解释.

Do not use the VARCHAR datatype. Use the VARCHAR2 datatype instead. Although the VARCHAR datatype is currently synonymous with VARCHAR2,the VARCHAR datatype is scheduled to be redefined as a separate datatype used for variable-length character strings compared with different comparison semantics.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读