Oracle中instr和substr存储过程详解
instr和substr存储过程,分析内部大对象的内容
instr函数
instr函数用于从指定的位置开始,从大型对象中查找第N个与模式匹配的字符串。
用于查找内部大对象中的字符串的instr函数语法如下:
dbms_lob.instr( lob_locinblob, patterninraw, offsetininteger:=1; nthininteger:=1) returninteger; dbms_lob.instr( lob_locinclobcharactersetany_cs, patterninvarchar2charactersetlob_loc%charset, offsetininteger:=1, nthininteger:=1) returninteger;
lob_loc为内部大对象的定位器
pattern是要匹配的模式
offset是要搜索匹配文件的开始位置
nth是要进行的第N次匹配
substr函数
substr函数用于从大对象中抽取指定数码的字节。当我们只需要大对象的一部分时,通常使用这个函数。
操作内部大对象的substr函数语法如下:
dbms_lob.substr( lob_locinblob, amountininteger:=32767, offsetininteger:=1) returnraw; dbms_lob.substr( lob_locinclobcharactersetany_cs, amountininteger:=32767, offsetininteger:=1) returnvarchar2charactersetlob_loc%charset;
其中各个参数的含义如下:
lob_loc是substr函数要操作的大型对象定位器
amount是要从大型对象中抽取的字节数
offset是指从大型对象的什么位置开始抽取数据。
如果从大型对象中抽取数据成功,则这个函数返回一个raw值。如果有一下情况,则返回null:
1任何输入参数尾null
2amount<1
3amount>32767
4offset<1
5offset>LOBMAXSIZE
示例如下:
declare source_lobclob; patternvarchar2(6):='Oracle'; start_locationinteger:=1; nth_occurrenceinteger:=1; positioninteger; buffervarchar2(100); begin selectclob_locatorintosource_lobfrommylobswherelob_index=4; position:=dbms_lob.instr(source_lob,pattern,start_location,nth_occurrence); dbms_output.put_line('Thefirstoccurrencestartsatposition:'||position); nth_occurrence:=2; selectclob_locatorintosource_lobfrommylobswherelob_index=4; position:=dbms_lob.instr(source_lob,pattern,start_location,nth_occurrence); dbms_output.put_line('Thefirstoccurrencestartsatposition:'||position); selectclob_locatorintosource_lobfrommylobswherelob_index=5; buffer:=dbms_lob.substr(source_lob,9,start_location); dbms_output.put_line('Thesubstringextractedis:'||buffer); end; / Thefirstoccurrencestartsatposition:8 Thefirstoccurrencestartsatposition:24 Thesubstringextractedis:Oracle9i
PL/SQL过程已成功完成。
以上所述是小编给大家介绍的Oracle中instr和substr存储过程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!