CREATE TABLE NYYTEST.LOB_TABLE

(

ID NUMBER(10) NOT NULL,

IN_LOB CLOB NULL,

OUT_LOB

CLOB NULL

)

/

Dbms_lob.read用于将lob类型的数据读入到缓冲区中

Dbms_lob.read(

Lob_loc IN BLOB/clob/bfile,  amount INOUT BINARY_INTEGER,  offset IN INTEGER,  buffer OUT RAW

)

lob_loc:要读取的LOB定位器  amount:要读取的字节数

offset:开始读取操作的偏移量

buffer:存储读操作结果的缓冲区

declare

lob_col clob;

buffer varchar2(2000);

amount int;

offset int;

begin

select out_lob into lob_col

from lob_table

where id=10;

offset:=1;--从第一位开始

amount:=dbms_lob.getlength(lob_col);--获取该字段存储的数据的长度

dbms_lob.read(lob_col,amount,offset,buffer);--读取内容到缓冲区

dbms_output.put_line(buffer);--缓冲区内容输出

end;

执行程序报错:numeric or value error string:characher string buffer too

small

查找错误原因:

对于10g以上版本(包括10g), dbms_output.put_line的最大长度限制是32767

在Oracle

10g之前,使用Dbms_Output.Put_Line进行输出调试,如果输出信息单行超过255个字符,则会提示错误

解决办法:采用循环的方式,每次读取200字符的内容

declare

lob_col clob;

buffer varchar2(2000);

amount int;

amount1 int;

offset int;

offset1 int;

VAR Number:=0;

num Number:=0;

begin

select out_lob into lob_col

from lob_table

where id=10;

offset:=1;

amount:=dbms_lob.getlength(lob_col);

amount1:=200;

var:=round(amount/amount1);---循环次数

loop

dbms_lob.read(lob_col,amount1,offset,buffer);---将内容赋给buffer变量

num:=num+1;

offset:=offset+200;

dbms_output.put_line(buffer);打印本次读取的内容

exit when num=var;

end loop;

end;

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐