前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OCCI处理CHAR类型字符串变量的不同

OCCI处理CHAR类型字符串变量的不同

作者头像
bisal
发布2019-01-29 12:02:16
8810
发布2019-01-29 12:02:16
举报

问题背景

一个旧应用,原先应用是用proc写的,9i的库,现在应用需要改为使用OCCI,其中有一段查询逻辑:select ... where upper(state)=upper(:1)。

(此处请不要纠结于where条件中state字段使用了upper函数,因为此表数据量很小,且其历史比较悠久,未建索引。)

对应表中定义的state字段类型是char(3),但此处查询条件变量的值可能是两位,例如'NY'。

现象

1. 使用sqlplus执行select ... where upper(state)=upper(:1)可以正常显示。

2. 使用sql developer执行select ... where upper(state)=upper(:1)可以正常显示。

3. 使用proc执行,可以正常显示。

4. 使用OCCI方式,执行,显示为空

解决

对于使用OCCI的方式,将其改写为:

1. select ... where trim(upper(state)) = trim(upper(:1));

2. select ... where upper(state) = upper(rpad(:1, 3, ' '));

原理推断

1. 首先char和varchar2类型的最大区别,就是char是定长类型,varchar2是不定长类型,网上包括官方文档有很多介绍了,用例子简单讲,就是:

create table test(

a char(25),

b varchar2(25)

);

insert into test values('a', b');

a字段存储的是“a+24个空格”,b字段存储的就是“b”。

可以从select a, length(a), b, length(b) from test;进一步验证。

即char会占用最大的存储空间,varchar2则只会存储实际占用的空间。

2. 从http://www.itpub.net/thread-1014651-1-1.html这篇帖子可以看出,和这个问题相同。推断是OCCI的bug导致。

虽然翻了OCCI的文档,并未找到对这个问题的解释,但从Oracle官方文档对填补空格比较字符串的语义说明,可以看出一些端倪: Blank-Padded Comparison Semantics If the two values have different lengths, then Oracle first adds blanks to the end of the shorter one so their lengths are equal. Oracle then compares the values character by character up to the first character that differs. The value with the greater character in the first differing position is considered greater. If two values have no differing characters, then they are considered equal. This rule means that two values are equal if they differ only in the number of trailing blanks. Oracle uses blank-padded comparison semantics only when both values in the comparison are either expressions of datatype CHAR, NCHAR, text literals, or values returned by the USER function. Nonpadded Comparison Semantics Oracle compares two values character by character up to the first character that differs. The value with the greater character in that position is considered greater. If two values of different length are identical up to the end of the shorter one, then the longer value is considered greater. If two values of equal length have no differing characters, then the values are considered equal. Oracle uses nonpadded comparison semantics whenever one or both values in the comparison have the datatype VARCHAR2 or NVARCHAR2. 即对于CHAR、NCHAR类型的字符串比较,Oracle首先会自动补齐空格,然后再一个字符一个字符地比较,不会因为空格数不同认为两者不同,且这个过程应该不是简单的trim()操作,因为如果字段有索引仍会使用。

对于VARCHAR2、NVARCHAR2类型的字符串比较,由于其不会自动存储空格,如果有空格,则也是作为有意义的存储,因此不存在上述问题。

综上所述,对于CHAR类型,不应该因为补空格位数的问题,作为比较的依据,除非使用的where a = trim('a'),人为对值进行处理,因此有理由怀疑OCCI对CHAR类型字符串的比较,至少和其他终端查询的逻辑不同,至于是不是bug,需要看看有没有官方的解释了

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年03月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档