oracle生成txt(二)
_region test_loadfile.region%type;
v_firstlocation number;
v_secondlocation number;
v_totalinserted number;
begin
if (p_path is null or p_filename is null) then
goto to_end;
end if;
v_totalinserted:=0;
/*open specified file*/
v_filehandle:=utl_file.fopen(p_path,p_filename,'r');
loop
begin
utl_file.get_line(v_filehandle,v_text);
exception
when no_data_found then
exit;
end ;
v_firstlocation:=instr(v_text,',',1,1);
v_secondlocation:=instr(v_text,',',1,2);
v_name:=substr(v_text,1,v_firstlocation-1);
v_addr_jd:=substr(v_text,v_firstlocation+1,v_secondlocation-v_firstlocation-1);
v_region:=substr(v_text,v_secondlocation+1);
/*插入数据库操作*/
insert into test_loadfile
values (v_name,v_addr_jd,v_region);
commit;
end loop;
<>
null;
end loadfiledata;
可以不用在init.ora中改的
只要用管理员的权限登陆,执行:
create directory UTL_FILE_TEST as '/*'
应该就可以了,不用新启动oracle的
不过其他用户要使用此目录要授权的
/
grant create any directory to scott;
grant create any library to scott;
create or replace directory utllobdir as 'C:/ep';
在initsid.ora文件中,加入或修改
设置utl_file_dir的要点:
1。 utl_file_dir=* 这表示你能操作任何目录,尽量不要用
2。 utl_file_dir=d:/ 这表示你能操作d:/目录下的文件,但你不能操作d:/目录下的子目录
3。注意在设置
utl_file_dir=路径时,如果路径是长路径名,例如c:/my temp目录,则你必须加上'',例如:
utl_file_dir='c:/my temp'
4。utl_file_dir可以是多个路径
utl_file_dir=c:/,d:/,d:/temp,'c:/my temp'
5。设置完必须重新启动数据库
(2)spool 在命令行中执行
set trimspool on
set linesize 550
set pagesize 2000
set newpage 1
set heading off
set term off
spool sp_test.txt
select xh||'|'||hphm1||'|'||hpzl1||'|'||ccdjrq1 from lifztest_analyse3;
spool off