求助 关于UTL_FILE 包的使用
求助 关于UTL_FILE 包的使用环境为oracle9i
initl.ora 中的最后两行是
#############################################
#utl_file 包的起用
#############################################
utl_file_dir=D:\oracle\oradata\rawdata,D:\oracle\oradata\errorlogs现在用hr用户执行这个程序sqlplus下DECLARE
  v_FileHandle UTL_FILE.FILE_TYPE;
BEGIN
  -- Open the file D:\oracle\oradata\rawdata\utl_file.tx for writing.  If the
  -- file does not exist, this will create it.  If the file
  -- does exist, this will overwrite it.
  v_FileHandle := UTL_FILE.FOPEN('D:\oracle\oradata\rawdata\', 'utl_file.txt', 'w');
  
  -- Write some lines to the file.
  UTL_FILE.PUT_LINE(v_FileHandle, 'This is line 1!');
  FOR v_Counter IN 2..11 LOOP
    UTL_FILE.PUTF(v_FileHandle, 'This is line %s!\n', v_Counter);
  END LOOP;
  
  -- And close the file.
  UTL_FILE.FCLOSE(v_FileHandle);
END;报错
ERROR 位于第 1 行:
ORA-06510: PL/SQL: 无法处理的用户自定义异常事件
ORA-06512: 在"SYS.UTL_FILE", line 120
ORA-06512: 在"SYS.UTL_FILE", line 204
ORA-06512: 在line 4

解决方案 »

  1.   

    环境:windows 2000 server + oracle 8.1.71.读文件举例:
    -- 环境 windows 2000 server + oracle 8.1.7
    -- 先在 init.ora中的参数utl_file_dir
    -- 例:  utl_file_dir=(d:\test,e:\\,e:\share)
    -- set serveroutput on size 1000000 format wrapped create or replace procedure read_txtfile( -- 读一个文本文件,并在sqlplus中显示其内容
    path   in varchar2,
    name   in varchar2
    )
    as
    l_output   utl_file.file_type;
    str        varchar2(1000);
    begin
    l_output:=utl_file.fopen(path,name,'r',2000); -- 每行最大字节数最多为32K bytes
    --l_output:=utl_file.fopen(path,name,'r'); -- 每行最大字节数最多为1023 bytes
    loop
    utl_file.get_line(l_output,str);
    dbms_output.put_line(str);
    end loop;
    exception
    when no_data_found then
    utl_file.fclose(l_output);
    when utl_file.invalid_path then
    raise_application_error(-20001,'INVALID_PATH!');
    when utl_file.invalid_mode then
    raise_application_error(-20002,'INVALID_MODE!');
    when utl_file.invalid_filehandle then
    raise_application_error(-20003,'INVALID_FILEHANDLE!');
    when utl_file.invalid_operation then
    raise_application_error(-20004,'INVALID_OPERATION!');
    when utl_file.read_error then
    raise_application_error(-20005,'READ_ERROR!');
    when utl_file.write_error then
    raise_application_error(-20006,'WRITE_ERROR!');
    when utl_file.internal_error then
    raise_application_error(-20007,'INTERNAL_ERROR!');
    when others then
    str:=sqlerrm(sqlcode);
    dbms_output.put_line(str);
    end;
    /--SQL> execute read_txtfile('d:\test','test.txt');
    --日本SONY(索尼) 1万/月,仅要研究生   
    --韩国三星电子中国总部 25万/年   
    --法国索姆软件,年薪20万/年,赴欧工作   
    --美国Cisco(思科)15000/月,仅要研究生   
    --美国INTEL(英特尔) 13000/月   
    --美国IBM 5000左右/月   
    --德国西门子 8000 /月  --PL/SQL 过程已成功完成。
    2.写文件举例
    -- 环境 windows 2000 server + oracle 8.1.7
    -- 先在 init.ora中的参数utl_file_dir
    -- 例:  utl_file_dir=(d:\test,e:\\,e:\share)
    -- set serveroutput on size 1000000 format wrapped create or replace procedure write_txtfile( -- 写一个字符串到指定文本文件中
    path   in varchar2,
    name   in varchar2,
    pstr   in varchar2
    )
    as
    l_output   utl_file.file_type;
    str        varchar2(1000);
    begin
    l_output:=utl_file.fopen(path,name,'a',2000); -- 每行最大字节数最多为32K bytes
    --l_output:=utl_file.fopen(path,name,'a'); -- 每行最大字节数最多为1023 bytes
    utl_file.put_line(l_output,pstr);
    utl_file.fclose(l_output);
    exception
    when utl_file.invalid_path then
    raise_application_error(-20001,'INVALID_PATH!');
    when utl_file.invalid_mode then
    raise_application_error(-20002,'INVALID_MODE!');
    when utl_file.invalid_filehandle then
    raise_application_error(-20003,'INVALID_FILEHANDLE!');
    when utl_file.invalid_operation then
    raise_application_error(-20004,'INVALID_OPERATION!');
    when utl_file.read_error then
    raise_application_error(-20005,'READ_ERROR!');
    when utl_file.write_error then
    raise_application_error(-20006,'WRITE_ERROR!');
    when utl_file.internal_error then
    raise_application_error(-20007,'INTERNAL_ERROR!');
    when others then
    str:=sqlerrm(sqlcode);
    dbms_output.put_line(str);
    end;
    /
    --SQL> execute write_txtfile('e:\','njhart2003.txt','hello oracle,i like oracle!');