Step by Step Reading and Writing A Text File Using UTL
Step by Step Reading and Writing A Text File Using UTL
UTL_FILE is an oracle pl/sql package that is supplied to allow PL/SQL to read and
create text files in the file system.
UTL_FILE can only read and create text files. Specifically, it cannot be used to read
or create binary files.
UTL_FILE is an appropriate tool for creating reports in the form of flat file from the
database . UTL_FILE is also used for reading files.
In the above query utl_file_dir is the logical name for the path E:\PLSQL.
We can mention the logical name utl_file_dir inside the program in uppercase within
single quotes (utl_file_dir is mapped to the directory E:\PLSQL)
We can create any number of logical path names(DBA directories) in oracle 10g.
Null?
Type
EMPNO
NUMBER(10)
ENAME
VARCHAR2(15)
JOB
VARCHAR2(15)
MGR
NUMBER(4)
HIREDATE
DATE
SAL
NUMBER(7,2)
Comma1 varchar(10);
Comma2 varchar(10);
Comma3 varchar(10);
Comma4 varchar(10);
Comma5 varchar(10);
f_empno emp.empno%type;
f_ename emp.ename%type;
f_job emp.job%type;
f_mgr emp.mgr%type;
f_hiredate emp.hiredate%type;
f_sal emp.sal%type;
begin
f_dir := E:\PLSQL;
fname := input.txt;
f := utl_file.fopen(UTL_FILE_DIR,fname,r'); opening the file using fopen function
loop
begin
utl_file.get_line(f,f_line);
using a loop continuously get the files content using get_line function
exception
when no_data_found then
exit;
end;
Comma1 := INSTR(f_line, , ,1 , 1);
Comma2 := INSTR(f_line, , ,1 , 2);
ENAME
JOB
MGR
HIREDATE
SAL
7369
SMITH
CLERK
7902
17-DEC-80
800
7499
ALLEN
ANALYST
7698
20-FEB-81
2850
7521
WARD
SALESMAN
7698
22-FEB-81
1250
7566
JONES
MANAGER
7839
02-APR-81
2975
7654
MARTIN
28-SEP-81
1250
SALESMAN
7698
Example 2:
The following is the procedure to write a database table contents to a text file.
This PL/SQL Procedure write the contents of the database table emp in the scott
schema to a text file called emp_table.txt in the windows directory E:\PLSQL
SQL> conn scott/tiger
Connected.
SQL> show user
USER is SCOTT
Step 1)Execute the following procedure.
create or replace procedure write_file is
file1 utl_file.file_type;
cursor empc is
select * from emp;
employ empc%rowtype;
stmt varchar2(300);
head varchar2(300);
line varchar2(300);
begin
file1 := utl_file.fopen(UTL_FILE_DIR,'emp_table.txt,'w);
utl_file.put_line(file1,Report Generated on: || sysdate);
utl_file.new_line(file1);
head:=EMPNO
HIREDATE
ENAME
SAL
JOB
MGR
COMM
DEPTNO;
UTL_FILE.PUTF(file1, head);
utl_file.new_line(file1);
line:=
==================================================================
================================;
UTL_FILE.PUTF(file1, line);
utl_file.new_line(file1);
for employ in empc loop
stmt := rpad(employ.empno,10, ) ||
rpad(employ.ename,20, ) ||
rpad(employ.job,20, ) ||
rpad(nvl(to_char(employ.mgr), ),30, ) ||
rpad(employ.hiredate,30, ) ||
rpad(employ.sal,30, ) ||
rpad(nvl(to_char(employ.comm), ),25, ) ||
rpad(employ.deptno,8, );
utl_file.PUTF(file1, stmt);
utl_file.new_line(file1);
end loop;
utl_file.fclose(file1);
end;
/
Procedure created.
SQL> execute write_file;