1818IP-服务器技术教程,云服务器评测推荐,服务器系统排错处理,环境搭建,攻击防护等

当前位置:首页 - 数据库 - 正文

君子好学,自强不息!

Oracle存储过程读写文件

2022-11-19 | 数据库 | gtxyzz | 526°c
A+ A-

Oracle存储过程读写文件是实现文件与数据库表之间交互的重要手段,下面就为您详细介绍Oracle存储过程读写文件方面的知识,希望对您能有所帮助。

有时候我们需要在文件与数据库表之间利用程序来实现两者的交互,这里可以利用UTL_FILE包实现对文件的I/O操作.下面就分别介绍文件写表以及表数据写文件.

[1]表信息导出到文件

在SUSE上建议一个文件夹/home/zxin10/file,然后对其chmod g+w file进行授权(否则无法导出到文件),再对您指定的路径(/home/zxin10/file)向Oracle的系统表sys.dir$进行注册(否则也是无法成功将信息导出到文件),操作完后可以查询sys.dir$可以看到表中的OS_PATH中有您指定的路径位置.

注册方式:执行SQL语句create or replace directory BBB as ‘/home/zxin10/file’; 即可

存储过程如下:(写文件时,文件名可以不用先创建,程序中会自动创建指定文件)

CREATEORREPLACEPROCEDUREV3_SUB_FETCH_TEST_2 
( 
V_TEMPVARCHAR2, 
--1为成功,0为失败 
v_retvalueOUTNUMBER 
) 
AS 
--游标定义 
typeref_cursor_typeisREFCURSOR; 
cursor_selectref_cursor_type; 
select_cnamevarchar2(1000); 
 
v_file_handleutl_file.file_type; 
 
v_sqlvarchar2(1000); 
v_filepathVarchar2(500); 
v_filenameVarchar2(500); 
--缓冲区 
v_resultsVarchar2(500); 
 
v_pidvarchar2(1000); 
v_cpcnshortnameVarchar2(500); 
 
begin 
v_filepath:=V_TEMP; 
ifv_filepathisnullthen 
v_filepath:='/home/zxin10/file3'; 
endif; 
v_filename:='free_'||substr(to_char(sysdate,'YYYYMMDDHH24MI'),1,10)||'.all'; 
--游标开始 
select_cname:='selectcpid,cpcnshortnamefromzxdbm_ismp.scp_basic'; 
--打开一个文件句柄,同时fopen的***个参数必须是大写 
v_file_handle:=utl_file.fopen('BBB',v_filename,'A'); 
Opencursor_selectForselect_cname; 
Fetchcursor_selectintov_pid,v_cpcnshortname; 
Whilecursor_select%Found 
Loop 
v_results:=v_pid||'|'||v_cpcnshortname; 
--将v_results写入文件 
utl_file.put_line(v_file_handle,v_results); 
Fetchcursor_selectintov_pid,v_cpcnshortname; 
EndLoop; 
 
Closecursor_select;--关闭游标 
utl_file.fClose(v_file_handle);--关闭句柄 
v_retvalue:=1; 
exceptionwhenothersthen 
v_retvalue:=0; 
endV3_SUB_FETCH_TEST_2;

[2]将文件信息导入到表中

和上面一样,先对指定文件路径进行chmod,然后想Oracle的sys.dir$进行路径注册.

文件zte.apsuic位于/home/zxin10/file下,其数据格式: 1|22|cheng 2|33|zhou 3|44|heng 4|55|yaya

表LOADDATA脚本:

--Createtable 
createtableLOADDATA 
( 
IDVARCHAR2(50), 
AGEVARCHAR2(50), 
NAMEVARCHAR2(50) 
) 
/

程序如下:(读取文件时,指定文件名一定要预先存在,否则程序会失败)

createorreplacedirectoryBBBas'/home/zxin10/file'; 
/ 
--作用法是将特定的文件路径信息想Oracle注册(注册信息存放在sys.dir$表中) 

CREATEORREPLACEPROCEDUREV3_SUB_FETCH_TEST_3 
( 
--文件中的信息导入表中 
V_TEMPVARCHAR2, 
v_retvalueOUTNUMBER--1成功,0失败 
AS 
v_file_handleutl_file.file_type; 
v_sqlvarchar2(1000); 
v_filepathVarchar2(500); 
v_filenameVarchar2(500); 
--文件到表字段的映射 
v_idvarchar2(1000); 
v_agevarchar2(1000); 
v_namevarchar2(1000); 
--缓冲区 
v_strvarchar2(1000); 
--列指针 
v_inumber; 
--字符串定位解析指针 
v_sposition1number; 
v_sposition2number; 
begin 
v_filepath:=V_TEMP; 
ifv_filepathisnullthen 
v_filepath:='/home/zxin10/file'; 
endif; 
v_filename:='zte.apsuic'; 
--v_sql:='createorreplacedirectoryCCCas'''||v_filepath||''''; 
--executeimmediatev_sql; 
 
v_file_handle:=utl_file.fopen('CCC',v_filename,'r'); 
Loop 
--将文件信息读取到缓冲区v_str中,每次读取一行 
utl_file.get_line(v_file_handle,v_str); 
--dbms_output.put_line(v_str); 
--针对每行的列数 
v_i:=1; 
--针对字符串每次的移动指针 
v_sposition1:=1; 
--文件中每行信息3列,循环3次 
FORIIN1..3loop 
--当instr(v_str,'|',6)其中v_str为1|22|wuzhuocheng,它返回0 
v_sposition2:=instr(v_str,'|',v_sposition1); 
--字符串解析正常情况 
ifv_sposition2<>0then 
ifv_i=1then 
v_id:=substr(v_str,v_sposition1,v_sposition2-v_sposition1);--***列 
elsifv_i=2then 
v_age:=substr(v_str,v_sposition1,v_sposition2-v_sposition1);--第二列 
elsifv_i=3then 
v_name:=substr(v_str,v_sposition1,v_sposition2-v_sposition1);--第三列 
else 
return; 
endif; 
--字符串解析异常情况 
else 
ifv_i=1then 
v_id:=substr(v_str,v_sposition1);--***列 
elsifv_i=2then 
v_age:=substr(v_str,v_sposition1);--第二列 
elsifv_i=3then 
v_name:=substr(v_str,v_sposition1);--第三列 
else 
return; 
endif; 
endif; 
v_sposition1:=v_sposition2+1; 
v_i:=v_i+1; 
endloop; 
--每列循环完后将信息insertinto表中 
insertintozxdbm_ismp.loaddatavalues(v_id,v_age,v_name); 
EndLoop; 
--关闭句柄 
utl_file.fClose(v_file_handle); 
v_retvalue:=1; 
exceptionwhenothersthen 
v_retvalue:=0; 
endV3_SUB_FETCH_TEST_3; 

本文来源:1818IP

本文地址:https://www.1818ip.com/post/4594.html

免责声明:本文由用户上传,如有侵权请联系删除!

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。