Step by Step Reading and Writing A Text File Using UTL in Oracle
Step by Step Reading and Writing A Text File Using UTL in Oracle
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)
Connected.
SQL> show user
USER is SCOTT
SQL>create or replace procedure read_file is
f_line varchar2(2000);
f utl_file.file_type;
f_dir varchar2(250);
fname varchar2(50);
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);
Comma3 := INSTR(f_line, , ,1 , 3);
Comma4 := INSTR(f_line, , ,1 , 4);
Comma5 := INSTR(f_line, , ,1 , 5);
Each field in the input record is delimited by commas.
We need to find the location of two commas in the line.
and use the locations to get the field from the line.
f_empno := to_number(SUBSTR(f_line, 1, Comma1-1));
f_ename := SUBSTR(f_line, Comma1+1, Comma2-Comma1-1);
f_job := SUBSTR(f_line, comma2+1, Comma3-Comma2-1);
f_mgr := to_number(SUBSTR(f_line, comma3+1, Comma4-Comma3-1));
f_hiredate := to_date(SUBSTR(f_line, comma4+1, Comma5-Comma4-1),dd-mon-yyyy);
f_sal := to_number(SUBSTR(f_line, comma5+1),99999);
dbms_output.put_line(f_empno || || f_ename || || f_job || || f_mgr || || f_hiredate|| ||
f_sal);
insert into emp12 VALUES (f_empno,f_ename,f_job,f_mgr,f_hiredate,f_sal);
end loop;
utl_file.fclose(f);
commit;
end;
/
Procedure created.
SQL> execute read_file;
PL/SQL procedure successfully completed.
Step 4) check the emp12 table that the rows from external text file gets inserted or not.
SQL> select * from emp12;
EMPNO
ENAME
JOB
-
MGR
HIREDATE
SMITH
CLERK
7499
ALLEN
ANALYST
7698
20-FEB-81
2850
7521
WARD
SALESMAN
7698
22-FEB-81
1250
7566
JONES
7839
02-APR-81
2975
7654
MARTIN
SALESMAN
7698
17-DEC-80
7369
MANAGER
7902
SAL
28-SEP-81
800
1250
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
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;
PL/SQL procedure successfully completed.
Step 2) After execution of the above procedure a new file emp_table.txt is created in the
location E:\PLSQL as shown in the below screenshot.
Ads By Webfuii
dataQapps Technologies
Tags: Oracle 10g, package, reading and writing, step by step, text file, UTL_FILE,
UTL_FILE_DIR