ORALCE——utl_file包进行I/O操作

测试环境:

ORACLE版本:ORACLE 9i,

操作系统:windows XP

机器型号:lenovo b470


1、通过sys用户新建一个存放数据文件的direcotry。

SELECT * FROM DBA_DIRECTORIES;
CREATE OR REPLACE DIRECTORY dir_path AS 'D:\';

2、在SCOTT用户下新建一测试表 DIR

CREATE TABLE DIR AS SELECT * FROM HR.EMPLOYEES;

--多次执行
INSERT INTO DIR AS SELECT * FROM DIR

SQL> select count(*) from dir;

  COUNT(*)
----------
    876544

SQL>

3、编写匿名块

declare
  outfile       utl_file.file_type;  -- 定义数据
  begTime       date;
  endTime       date;
  totalSec      number;
begin
  
  -- 开始时间
  select sysdate into begTime from dual;
  dbms_output.put_line('---------begin---------'||to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));
  
  outfile := utl_file.fopen('DIR_PATH', 'dir.txt', 'W');
  for rec in (select employee_id, first_name, last_name, email, phone_number
                from scott.dir) loop
    utl_file.put_line(outfile,
                      rec.employee_id || ',' || rec.first_name || ',' ||
                      rec.last_name || ',' || rec.email || ',' ||
                      rec.phone_number);
  end loop;
  utl_file.fclose(outfile);
  
  -- 结束时间
  select sysdate into endTime from dual;
  
  -- 运行时间
  totalSec := (endTime-begTime)*86400;
  
  dbms_output.put_line('---------end---------'||to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));
  dbms_output.put_line('---------total---------'||totalSec||'--s');
end;

4、sys用户执行

你可能感兴趣的:(ORALCE——utl_file包进行I/O操作)