解决比较Oracle中CLOB字段问题

 

Oracle中CLOB和BLOB字段虽说在开发中满足了存放超大内容的要求,但是在一些简单使用中确频频带来麻烦。CLOB中存放的是指针,并不能直接取到实际值。而SQLServer中的text字段就很方便,可以直接拿来与需要的字符串比对,象什么等于呀小于呀Like呀不在话下。可是换成Oracle就麻烦死了,要开辟一个缓存,把内容一段段读取出来后转换,难道写个where条件都这么复杂?经过多方寻求资料,终于发现一个方便简单的方法:利用dbms_lob 包中的方法(放心,内置的)instr和substr 。具体的介绍如下:

instr函数与substr函数

instr函数用于从指定的位置开始,从大型对象中查找第N个与模式匹配的字符串。 
用于查找内部大对象中的字符串的instr函数语法如下:

1:  dbms_lob.instr(
2:  lob_loc in clob character set any_cs,
3:  pattern in varchar2 character set lob_loc%charset,
4:  offset in integer:=1,
5:  nth in integer := 1)
6:  return integer; 
7:   

lob_loc为内部大对象的定位器 
pattern是要匹配的模式 
offset是要搜索匹配文件的开始位置 
nth是要进行的第N次匹配

substr函数 
substr函数用于从大对象中抽取指定数码的字节。当我们只需要大对象的一部分时,通常使用这个函数。 
操作内部大对象的substr函数语法如下:

其中各个参数的含义如下: 
lob_loc是substr函数要操作的大型对象定位器 
amount是要从大型对象中抽取的字节数 
offset是指从大型对象的什么位置开始抽取数据。

如果从大型对象中抽取数据成功,则这个函数返回一个 raw 值。如果有一下情况,则返回null: 
1 任何输入参数尾null 
2 amount < 1 
3 amount > 32767 
4 offset < 1 
5 offset > LOBMAXSIZE

 1:  dbms_lob.instr(
 2:  lob_loc in blob, 
 3:  pattern in raw, 
 4:  offset in integer := 1;
 5:  nth in integer := 1)
 6:  return integer; 
 7:  dbms_lob.substr(
 8:    lob_loc in blob, 
 9:    amount in integer := 32767,
10:    offset in integer := 1)
11:  return raw; 
12:  dbms_lob.substr(
13:    lob_loc in clob character set any_cs, 
14:    amount in integer := 32767,
15:    offset in integer := 1)
16:  return varchar2 character set lob_loc%charset; 

照下面示例,很容易看懂:

 1:  declare 
 2:     source_lob clob;
 3:     pattern varchar2(6) := 'Oracle';
 4:     start_location integer := 1;
 5:     nth_occurrence integer := 1;
 6:     position integer;
 7:     buffer varchar2(100);
 8:  begin
 9:     select clob_locator into source_lob from mylobs where lob_index = 4;
10:     position := dbms_lob.instr(source_lob, pattern, start_location, nth_occurrence);
11:     dbms_output.put_line('The first occurrence starts at position:' || position);
12:   
13:     nth_occurrence := 2;
14:     select clob_locator into source_lob from mylobs where lob_index = 4;
15:     position := dbms_lob.instr(source_lob, pattern, start_location, nth_occurrence);
16:     dbms_output.put_line('The first occurrence starts at position:' || position);
17:   
18:     select clob_locator into source_lob from mylobs where lob_index = 5;
19:     buffer := dbms_lob.substr(source_lob, 9, start_location);
20:     dbms_output.put_line('The substring extracted is: ' || buffer);
21:  end; 

输出结果为: 
The first occurrence starts at position:8 
The first occurrence starts at position:24 
The substring extracted is: Oracle 9i

哈哈,轻松搞定CLOB字段的比对匹配筛选,不用一大堆的存储过程和函数,祝大家好运!

解决比较Oracle中CLOB字段问题的更多相关文章

  1. oracle中clob字段的使用

    oracle中定义了一个字段是clob的,由于用的是ssh的框架,结果在面向对象存取的时候出现clob类型字段和String类型字段的转换问题.开始查阅了clob字段和String字段的相互转换的方法 ...

  2. oracle中clob字段怎么查询非空列_20180517

    select * from uap_groupsynlogvo a where a.log_msg is not null ; 附加demo的建表脚本跟业务数据. 链接:https://pan.bai ...

  3. Oracle中Clob类型处理解析:ORA-01461:仅可以插入LONG列的LONG值赋值

    感谢原作者:破剑冰-Oracle中Clob类型处理解析 上一篇分析:ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 最近为Clob字段在插入数据时发现当字符的字节数(一个半角字符一 ...

  4. java 存储oracle的clob字段

    项目中有很长的字符创需要存储,用到了oracle的clob字段,直接很长的字符串插入到clob字段中会报字符过长的异常,于是便寻求解决方案.看到这个博客写的还不错 首先,创建一个含CLOB字段的表: ...

  5. .Net处理Oracle中Clob类型字段总结

    最近在做项目中用到Clob这个字段,Clob是存储无限长字符的Oracle字段,用的时候网上找资料找了好久,内容不是很多,大部分都不能用,当然也有可以用的,测试了不同版本,整理了一下,给大家在做项目的 ...

  6. Oracle中Clob类型处理解析

    最近利用NHibernate映射类型为Clob字段在插入数据时发现当字符的字节数(一个半角字符一个字节,一个全角字符两个字节)在2000-4000之间时报错(ORA-01461:仅可以插入LONG列的 ...

  7. Oracle中Clob类型处理解析 (转)

    转:原文:http://blog.csdn.net/pojianbing/article/details/2789426      最近利用NHibernate映射类型为Clob字段在插入数据时发现当 ...

  8. java 将长度很长的字符串(巨大字符串超过4000字节)插入oracle的clob字段时会报错的解决方案

    直接很长的字符串插入到clob字段中会报字符过长的异常,相信大家都会碰到这种情况 String sql = "insert into table(request_id,table_name, ...

  9. Oracle数据库clob字段导出为sql insert插入语句

    oracle数据库的clob字段导出为sql insert插入语句可以分三种情况:1,clob没有换行符:2,clob有换行符但不以分号结尾:3,clob有换行符并且以分号结尾. clob没有换行符使 ...

随机推荐

  1. zookeeper[5] zookeeper集群配置及伪集群配置

    参考:http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html 集群配置: 1.配置文件conf/zoo.cfg,除了单机模式的配置之外, ...

  2. iOS 后台定位被拒注意事项

    iOS 后台定位被拒的原因很简单就是没有达到苹果对后台定位的要求. 本地要求: 1.在plist文件中添加字段 "Privacy - Location Always Usage Descri ...

  3. [深入React] 7.组件生命周期

    生命周期一共分三段:初始化,运行中,销毁.按照顺序: 初始化 getDefaultProps():Object 全局只会调用一次,为当前类生成的默认props,会被父组件传入的同名props覆盖. g ...

  4. 为什么要使用Nginx?

    这里做了些基准测试表明nginx打败了其它的轻量级的web服务器和代理服务器,同样也赢了相对不是那么轻量级的产品. 有人说这些基准测试是不准确的,因为在这样那样的环境下,做的比较不一致.我倾向同意基准 ...

  5. HDU 2435 There is a war (网络流-最小割)

    There is a war Problem Description       There is a sea.       There are N islands in the sea.       ...

  6. mysql安装出现error Nr.1045

    我们在windows下安装mysql时会出现Access denied for user 'root'@localhost'(using password:No)的问题,这个问题是因为你的机器上之前安 ...

  7. Vss服务端用户存在,但客户端登陆不进去

    打开客户端Vss提示“Cannot find SS.INI file for user userName”,这个错误是找不到用户userName的SS.INI文件. 解决办法 在服务器上找到Vss共享 ...

  8. SVN服务器的本地搭建和使用

    用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用. 详细步骤如下 http://www.2cto.com/os/201412/361931.html

  9. QQ 国际版(International version) - 关闭弹出资讯

    1,打开QQ面板,点击左下角的 "企鹅"图标.选择 "Setting". 2,在弹出的 "Setting"面板中,选择 "Priv ...

  10. Catel帮助手册-Catel.Core:(1)参数检查

      我们检查方法是否正确,一般是返回对错,或者是是否抛出一个异常,大部分人不检查异常的正确性,那么这种错误在很深的堆栈中,很难查看. Catel与一般的检查方法不同,一般是使用   public vo ...