Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1973490
  • 博文数量: 148
  • 博客积分: 7697
  • 博客等级: 少将
  • 技术积分: 3071
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-10 23:04
个人简介

MiBDP,数据开发、项目团队、数据应用和产品在路上,金融保险、互联网网游、电商、新零售行业、大数据和AI在路上。对数仓、模型、ETL、数据产品应用了解。DTCC 2013演讲嘉宾,曾做过两款大获好评的数据产品平台。知识星球ID:35863277

文章分类
文章存档

2020年(1)

2019年(2)

2017年(2)

2016年(5)

2015年(1)

2014年(1)

2013年(6)

2012年(5)

2011年(24)

2010年(28)

2009年(1)

2008年(6)

2007年(30)

2006年(36)

分类: Oracle

2010-10-21 18:08:33


--首先我们创建用于文件操作的目录

SQL> create directory colin_dir as '/opt/oracle/oradata/orcl/colin';
 
Directory created
PS:/opt/oracle/oradata/orcl/colin这个目录必须在服务器上创建
--第二步授予读写权限

SQL> grant read,write on directory colin_dir to dba;
 
Grant succeeded
偶是直接把这个目录的读写权限授给了DBA这个角色,因为我当前的用户就是DBA,也可以通过sys用户直接授予当前用户
[oracle@localhost colin]$ sqlplus /nolog

SQL*Plus: Release 10.2.0.3.0 - Production on Thu Oct 21 17:34:36 2010

Copyright (c) 1982, 2006, Oracle. All Rights Reserved.

SQL> conn /as sysdba
Connected.
SQL> grant read,write on directory COLIN_DIR to gamedata_fs3;

Grant succeeded.
--第三步创建一个简单的写数据到文件excel的过程

create or replace procedure test_uti_file(test_file in varchar2) is
  v_content char(4);
  cursor test_ufile_cur is
    select id || chr(9) || name || chr(9) from uti_file_test;
  l_file utl_file.file_type;
begin
  l_file := utl_file.fopen('COLIN_DIR', test_file, 'w');

  open test_ufile_cur;

  loop
    fetch test_ufile_cur
      into v_content;
    exit when test_ufile_cur %notfound;
    utl_file.put_line(l_file, v_content, false);
  end loop;

  close test_ufile_cur;
  utl_file.fflush(l_file);
  utl_file.fclose(l_file);
end;
PS:上面的COLIN_DIR的目录名必须大写,test_file文件不必事先建立
--第四步测试过程

begin
  -- Call the procedure

  test_uti_file(test_uti_file.xls);
end;
--最后可以到服务器查看生成的test_uti_file.xls文件了


1    a
2    b


阅读(5020) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

godplayworld2012-03-13 12:38:54

你这个就是用chr(9)分开的文本文件吧,不是直正的excel

chinaunix网友2010-10-22 11:09:40

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com