Hi DBA-Mates,
This is very important for any Developer or DBA about to either read or write a text file in oracle using UTL_FILE packages.
This is very conceptual and tricky also. I remembered there was a session going on for NIT and IIT students in which very few only solved this problem. I would like to share it here, which may useful for all.
Notes:
• 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 is an appropriate tool for creating reports in the form of flat file from the database . UTL_FILE is also used for reading files.
• UTL_FILE can only read and create text files. Specifically, it cannot be used to read or create binary files. Please find these steps which must be followed in order to run UTL_FILE package.
Step 1) Connect as sys database user
SQL> sqlplus '/as sysdba'
Step 2) Create a directory:
SQL> CREATE OR REPLACE DIRECTORY utl_file_dir AS ‘E:\PLSQL’;
Directory created.
• 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.
Step 3) Grant read and write on the directory
SQL> grant read,write on directory utl_file_dir to scott;
Grant succeeded.
SQL>CONNECT SCOTT/scott-password
Example 1: The following is the procedure to read a text file in the file system and inserting the text file contents(rows) into a table that is present in oracle 10g database.
Step 1) Create a text file called input.txt in windows directory ‘E:\PLSQL’ as shown in the below screen shot.
Step 2) Check whether any contents present in emp12 table.
SQL>CREATE TABLE EMP12(EMPNO NUMBER(10), ENAME VARCHAR2(15), JOB VARCHAR2(15), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2));
Table created.
SQL> desc emp12;
Name Null? Type
————– ———– ———————–
EMPNO NUMBER(10)
ENAME VARCHAR2(15)
JOB VARCHAR2(15)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
SQL> select * from emp12;
no rows selected
Step 3) Execute the following procedure.
This PL/ SQL procedure will insert data(rows) from the text file called ‘input.txt’ to the database table emp12 in scott schema.
SQL> conn scott/scott-password
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 file’s 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.
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 ENAME JOB MGR HIREDATE SAL 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’ .
Step 3) Open the text document emp_table.txt to view the output.
If you have any doubts or errors, please contact us @https://ora-data.blogspot.in/Contact Us or write in comments. We will be happy to hear from you. Hope it will be useful and clear.
Hope below links useful:
ORACLE DATAGUARD CONCEPTS
RAC Concepts and Details
Jobs Tips
ORACLE APPS
CMCLEAN.sql Scripts and Details
This is very important for any Developer or DBA about to either read or write a text file in oracle using UTL_FILE packages.
This is very conceptual and tricky also. I remembered there was a session going on for NIT and IIT students in which very few only solved this problem. I would like to share it here, which may useful for all.
Notes:
• 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 is an appropriate tool for creating reports in the form of flat file from the database . UTL_FILE is also used for reading files.
• UTL_FILE can only read and create text files. Specifically, it cannot be used to read or create binary files. Please find these steps which must be followed in order to run UTL_FILE package.
Step 1) Connect as sys database user
SQL> sqlplus '/as sysdba'
Step 2) Create a directory:
SQL> CREATE OR REPLACE DIRECTORY utl_file_dir AS ‘E:\PLSQL’;
Directory created.
• 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.
Step 3) Grant read and write on the directory
SQL> grant read,write on directory utl_file_dir to scott;
Grant succeeded.
SQL>CONNECT SCOTT/scott-password
Example 1: The following is the procedure to read a text file in the file system and inserting the text file contents(rows) into a table that is present in oracle 10g database.
Step 1) Create a text file called input.txt in windows directory ‘E:\PLSQL’ as shown in the below screen shot.
Step 2) Check whether any contents present in emp12 table.
SQL>CREATE TABLE EMP12(EMPNO NUMBER(10), ENAME VARCHAR2(15), JOB VARCHAR2(15), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2));
Table created.
SQL> desc emp12;
Name Null? Type
————– ———– ———————–
EMPNO NUMBER(10)
ENAME VARCHAR2(15)
JOB VARCHAR2(15)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
SQL> select * from emp12;
no rows selected
Step 3) Execute the following procedure.
This PL/ SQL procedure will insert data(rows) from the text file called ‘input.txt’ to the database table emp12 in scott schema.
SQL> conn scott/scott-password
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 file’s 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 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 SALESMAN
7698 28-SEP-81
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 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 ENAME JOB MGR HIREDATE SAL 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’ .
Step 3) Open the text document emp_table.txt to view the output.
If you have any doubts or errors, please contact us @https://ora-data.blogspot.in/Contact Us or write in comments. We will be happy to hear from you. Hope it will be useful and clear.
Hope below links useful:
ORACLE DATAGUARD CONCEPTS
RAC Concepts and Details
Jobs Tips
ORACLE APPS
CMCLEAN.sql Scripts and Details
Regards,
I'm really loving the theme/design of your website. Do you ever run into any browser compatibility issues?
ReplyDeleteA small number of my blog readers have complained about my website not operating correctly in Explorer but looks great
in Opera. Do you have any solutions to help fix this problem?
DeleteDear User,
IE almost create problem for any website.
We need to check codes because I remembered when I was doing coding , IE was worst for compatibility. Also, Easy to modify the blog if you are using any google blog theme. But it depends on what type of blog you using.
I have idea/suggestions as per my experiences:
1. Please avoid to change the theme code of blogs.
2. Keep it simple.
3. Do customize very carefully.
4. Update IE version and check.
5. Login to IE/Bing Mater tool and submit you blog/website.
Hope these above points may useful.
Regards,
ora-data Team
Whats up very cool web site!! Guy .. Beautiful ..
ReplyDeleteWonderful .. I will bookmark your website and take the feeds also?
I am glad to seek out numerous useful info here within the post,
we want work out more techniques on this regard, thanks for sharing.
. . . . .
Dear User,
DeleteThank you for your feedback.
Please follow us on FB for latest updates:
Facebook Page
Regards,
ora-data Team
What i do not understood is in fact how you are not really a lot more neatly-preferred than you may be now.
ReplyDeleteYou're so intelligent. You already know thus significantly in relation to this topic, made me personally consider
it from so many varied angles. Its like men and women aren't fascinated until it's one thing to accomplish with
Lady gaga! Your personal stuffs outstanding. At all times maintain it up!
Hi there, of course this piece of writing is really fastidious and I have learned lot of
ReplyDeletethings from it about blogging. thanks.
Hi there friends, how is everything, and what you desire to say
ReplyDeleteregarding this post, in my view its actually awesome in support of me.
I'll immediately grab your rss feed as I can't
ReplyDeletein finding your email subscription hyperlink or newsletter service.
Do you have any? Please let me recognise so that I could subscribe.
Thanks.
DeleteDear User,
Thank you for your great interest and feedback.
You can please follow us on Facebook and google +
GOOGLE+
Regards,
ora-data Team
Spot on with this write-up, I honestly feel this amazing site needs far more attention.
ReplyDeleteI'll probably be returning to see more, thanks for the advice!
DeleteDear User,
Yeah sure, we are trying to give effort and make great posts which is easily understandable.
Regards,
ora-data Team
This paragraph is in fact a good one it assists new web people, who are wishing
ReplyDeletein favor of blogging.
DeleteDear User,
Thank you for your feedback. It really means for us.
Regards,
ora-data Team
ReplyDeleteDear User,
We used to do it manually.
We review the all comments, even we also used to get many spam.
We remove only such comments which is related very bad or wrong comments.
Regards,
ora-data Team
It is perfect time to make some plans for the future and it is time
ReplyDeleteto be happy. I've read this post and if I could I wish to suggest
you few interesting things or advice. Maybe you can write next articles referring to this article.
I want to read even more things about it!
DeleteDear User,
Thank you for your feedback.
Sure, we will try our best.
Regards,
ora-data Team
It's perfect time to make some plans for the future and it
ReplyDeleteis time to be happy. I've learn this publish and if I could I desire to
suggest you few attention-grabbing things or tips.
Maybe you can write subsequent articles referring to this article.
I wish to learn more things approximately it!
Hey are using Wordpress for your blog platform? I'm new to the blog
ReplyDeleteworld but I'm trying to get started and set up my own. Do you require any html coding expertise to make your own blog?
Any help would be really appreciated!
DeleteDear User,
I am using google blogspot as you can see in url. Thanks for you feedback btw our team have multiple knowledge sharing like html and dba etc...
Regards,
ora-data Team
ReplyDeleteDear User,
We approve manually our comments. And we try to help people if they are posting related to education or informational.
Some spam comes which we use to delete manually.
Regards,
ora-data Team
What's up to all, the contents existing at this website are truly remarkable
ReplyDeletefor people knowledge, well, keep up the nice work fellows.
Dear User,
DeleteThank you for your valuable feedback.
Regards,
ora-data Team
Hellо my familʏ member! I wish to say that this
ReplyDeletepost is awesome, nice written and include almost all significant infos.
I would llike to see more posts like this .
I'm impressed, I must say. Rarely do I encounter
ReplyDeletea blog that's both educative and interesting, and let me tell
you, you have hit the nail on the head. The issue is an issue that too few people are speaking intelligently about.
I'm very happy that I stumbled across this during my search for something relating to
this.
I am realⅼy inspiredd together wіtһ your writing talenys aѕ ԝell as with the layout oon үoսr blog.
ReplyDeleteΙs that this ɑ pid subject matter ⲟr did you customize it your sеlf?
Eitһer wɑy қeep upp the excellent qualuty writing, іt is uncommon t᧐ lооk а great weblog like thiѕ one theѕe
days..
Ꮋі to aⅼl, for thе reason tһat І am genuinely eager
ReplyDeleteof reading thіs blog's post to be updated daily. Ӏt
consosts ᧐f fastidious informаtion.
Its like yoս read my mind! You aрpear to know a lot aboᥙt this, liкe you wrote thee
ReplyDeletebook іn it or something. I think tһat you cold Ԁo wіth some pics t᧐ drive thе message home а bit, Ƅut insteаd оf that, this is magnificent
blog. A fantastic гead. I'll defіnitely be back.
Ꮩery ɡood article. I dеfinitely appreciatee tһis site.
ReplyDeleteKеep writing!
Is there any oracle function similar to JSON_VALUE to read data from a text file which is stored as BLOB. Text file is separated with semicolon delimiter.
ReplyDelete