Oracle操作大对象CLOB

--创建测试表

create table test1 (    tid int primary key,    tname varchar2(20),    tcontent clob )

create sequence sequ_test1

--插入数据 insert into test1 values (sequ_test1.nextval,'第一个文件','heheh')

--使用系统内部的文件读写函数。

--1. 创建一个Oracle能够管理的磁盘目录(Oracle系统中存放的系统数据全都都是大写形式)

create or replace directory test_dir   as 'e:/' select * from test1

--2.

declare     

  tempimg clob;--定义临时变量存放数据      

tempdir bfile := bfilename('TEST_DIR','a.txt');--非常重要:所有数据都是大写存放的     

  amount int:=dbms_lob.getlength(tempdir);     

  src_offset int:=1;      

dest_offset int:=1;    

   csid int:=0;      

lc int:=0;      

warning int; begin       

     insert into test1 values (sequ_test1.nextval,'第一个文件信息',empty_clob())             

  returning tcontent into tempimg;             --使用内置的包,给tempimg写入数据     

  dbms_lob.fileopen(tempdir);--打开指定文件         

    dbms_lob.loadclobfromfile(tempimg,tempdir,amount,dest_offset,src_offset,csid,lc,warning);         

    dbms_lob.fileclose(tempdir);--关闭文件        

     dbms_output.put_line('恭喜你,终于成功了!!!');            

commit;

end ;

--读取出来

declare     src clob;    

outfile utl_file.file_type;    

length integer;    

buffer varchar2(8000);

begin    

select tcontent into src from test1 where tid=1;      

   length := dbms_lob.getlength(src);    

    dbms_lob.read(src,length,1,buffer);         --打开磁盘文件    

outfile := utl_file.fopen('TEST_DIR','hello.sql','w',8000);       

  utl_file.put(outfile,buffer);--写入数据      

   utl_file.fclose(outfile); --关闭指针    

    dbms_output.put_line('文件已经写入完毕!');

end;

select * from test1

posted @ 2013-01-23 18:52  珊娃子。  阅读(283)  评论(0编辑  收藏  举报