日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

oracle 存储过程写文件,Oracle写本地文件

發(fā)布時(shí)間:2023/12/1 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 oracle 存储过程写文件,Oracle写本地文件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Oracle寫本地文件是指寫到運(yùn)行Oracle的主機(jī)上,而不是運(yùn)行該腳本的機(jī)器上。

說(shuō)起來(lái)有點(diǎn)拗口,實(shí)際上就是無(wú)論在哪里執(zhí)行這個(gè)過(guò)程,生成的文件始終都是在服務(wù)器上的。

下面過(guò)程實(shí)現(xiàn)了這個(gè)功能:

logdir是指文件存放路徑。有Oracle的directory指定:

create or replace directory log_dir? as '/oracle/admin/orcl';

創(chuàng)建帶有clob字段的表:

create table testclob(obj_type varchar2(255), obj_content clob);

插入測(cè)試數(shù)據(jù):

insert into testclob(obj_type, obj_content)

select 'PROCEDURE', dbms_metadata.get_ddl('PROCEDURE', o.object_name)

from dba_objects o

where o.owner = user

and o.object_type = 'PROCEDURE'

創(chuàng)建存儲(chǔ)過(guò)程:

create or replace procedure sp_writelog2file(logdir varchar2,

filename varchar2,

writemode char := 'W') as

type tbl_result is table of varchar2(2000) index by pls_integer;

v_res tbl_result;

type tbl_clob is table of clob index by pls_integer;

v_clobs?? tbl_clob;

v_filename?? varchar2(255) := filename;

v_logdir???? varchar2(255) := logdir;

v_buffer???? pls_integer := 2000;

v_offset???? pls_integer := 1;

v_filehandle utl_file.file_type;

function f_readclob(varclob clob)

return tbl_result as

v_maxbuff pls_integer := 2000;

v_cloblen pls_integer := length(varclob);

v_result tbl_result;

v_buffer pls_integer := v_maxbuff;

v_offset pls_integer := 1;

v_nextpos pls_integer := 1;

v_prevpos pls_integer := 1;

v_maxstep pls_integer := 20;

v_nth??? pls_integer := v_maxstep;

begin

while v_nextpos <> 0 loop

v_nextpos := dbms_lob.instr(varclob, chr(10), 1, v_nth);

v_buffer? := (case when v_nextpos = 0 then v_cloblen else v_nextpos end) - v_prevpos;

if (v_buffer > v_maxbuff and v_nextpos <> 0) then

v_nth := v_nth - 3;/*超過(guò)最大緩沖區(qū),指針退3個(gè)*/

elsif (v_buffer < 3*v_maxbuff/4 and v_nextpos <> 0) then

v_nth := v_nth + 3;/*未達(dá)最大緩沖區(qū)的3/4,指針進(jìn)3個(gè)*/

else

dbms_lob.read(varclob, v_buffer, v_offset, v_result(nvl(v_result.last, 0) + 1));

v_prevpos := v_nextpos;

v_nth := v_nth + v_maxstep;

v_offset := v_offset + v_buffer;

end if;

end loop;

return v_result;

end f_readclob;

begin

v_filehandle := utl_file.fopen(v_logdir, v_filename, writemode);

if(utl_file.is_open(v_filehandle)) then

select t2.obj_content

bulk collect into v_clobs

from testclob t2;

for i in 1 .. v_clobs.count loop

v_res := f_readclob(v_clobs(i));

for j in 1 .. v_res.count loop

utl_file.put_line(v_filehandle, v_res(j));

end loop;

end loop;

end if;

utl_file.fclose(v_filehandle);

exception when others then

utl_file.fclose(v_filehandle);

end sp_writelog2file;

調(diào)用存儲(chǔ)過(guò)程,將testclob的內(nèi)容寫到主機(jī)上的文件中:

call sp_writelog2file('LOG_DIR', 'lyon.txt');

對(duì)于clob對(duì)象的讀取,采用了分段截取的算法。截取標(biāo)識(shí)為換行符(chr(10))。

每次步長(zhǎng)為20個(gè)換行符間隔。假設(shè)每次截取長(zhǎng)度最大值為N(這里N=2000)。該間隔區(qū)間內(nèi),如果字符數(shù)范圍在[N*3/4, N]之間,則直接截取。

如果小于3/4 N長(zhǎng)度,則指針標(biāo)識(shí)向前推3個(gè)換行符間隔。如果大于N,則向后退3個(gè)換行符間隔。保證截取的長(zhǎng)度始終在3/4-1個(gè)N之間。

總結(jié)

以上是生活随笔為你收集整理的oracle 存储过程写文件,Oracle写本地文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。