ITPub博客

首页 > 数据库 > Oracle > utl_file简单使用

utl_file简单使用

原创 Oracle 作者:dasong2410 时间:2011-11-04 09:20:01 0 删除 编辑
--读文件
set serveroutput on;
declare
  l_dir       varchar2(100);
  l_file_name varchar2(100);
  l_mod       varchar2(10);
  l_file      utl_file.file_type;
  l_len       number;
  l_exist     boolean;
  l_buffer    varchar2(1024);
  l_blocksize number;
  l_cur_pos   number := 0;
begin
  l_dir := 'MARCUS_DUMP_DIR';
  l_file_name := 'data01.csv';
  l_mod := 'r';  
  utl_file.fgetattr(l_dir, l_file_name, l_exist, l_len, l_blocksize);
 
  if (l_exist) then
    l_file := utl_file.fopen(l_dir, l_file_name, l_mod);
    while(l_cur_pos < l_len) loop
      utl_file.get_line(l_file, l_buffer);
      dbms_output.put_line(l_buffer);
      l_cur_pos := utl_file.fgetpos(l_file);
    end loop;
 
    utl_file.fclose(l_file);
  end if;
end;
/

--写文件
declare
  l_dir       varchar2(100);
  l_file_name varchar2(100);
  l_mod       varchar2(10);
  l_file      utl_file.file_type;
  l_buffer    varchar2(1024);
begin
  l_dir := 'MARCUS_DUMP_DIR';
  l_file_name := 'data03.csv';
  l_mod := 'w'; 
  l_file := utl_file.fopen(l_dir, l_file_name, l_mod);
 
  for col_info in (select table_name, column_name, data_type
                     from user_tab_columns
                    order by table_name, column_name, data_type) loop
    l_buffer := col_info.table_name || ', ' || col_info.column_name || ', ' || col_info.data_type;                 
    utl_file.put_line(l_file, l_buffer);
  end loop;

--  utl_file.fflush(l_file);
  utl_file.fclose(l_file);
end;
/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7417681/viewspace-710206/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: oracle外部表示例
请登录后发表评论 登录
全部评论

注册时间:2011-05-12

  • 博文量
    66
  • 访问量
    349769