472,330 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

Writing data to file using UTL_FILE package

26
scribes
i would like to write /export the results of a query to a text file
logic
1)execute query and
2) export to a text file
Mar 12 '08 #1
21 14163
debasisdas
8,127 Expert 4TB
Try using the SPOOL command.
Mar 12 '08 #2
amitpatel66
2,367 Expert 2GB
Try something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> SET FEEDBACK OFF
  3. SQL> SET VERIFY OFF
  4. SQL> SET TERMOUT OFF
  5. SQL> SET HEADING OFF
  6. SQL> SPOOL C:/data.txt
  7. SQL> SELECT * FROM emp;
  8. SQL> SPOOL OFF
  9.  
  10.  
Now view your data file in C:\ Drive
Mar 12 '08 #3
change
26
Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE DIRECTORY LOGG_DIR AS 'C:\TEST'
  2. DECLARE
  3. fileID UTL_FILE.FILE_TYPE;
  4. LINE_BUFF VARCHAR2(1000);
  5. BEGIN
  6. fileID := UTL_FILE.FOPEN (LOGG_DIR, 'emp.dat', 'W');
  7. FOR emprec IN (SELECT * FROM SO_SERVICES)
  8. LOOP
  9. LINE_BUFF :=TO_CHAR (emprec.ACCT_NO) ;
  10. UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
  11. END LOOP;
  12. UTL_FILE.FCLOSE (fileID);
  13. END;
  14.  
  15. /
  16.  
ARE

R at line 2:
00901: invalid CREATE command
Mar 12 '08 #4
change
26
hi am using code like ths and get the error at the bottom

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE OR REPLACE DIRECTORY LOGG_DIR AS 'C:\TEST'
  3. DECLARE
  4. fileID UTL_FILE.FILE_TYPE;
  5. LINE_BUFF VARCHAR2(1000);
  6. BEGIN
  7. fileID := UTL_FILE.FOPEN (LOGG_DIR, 'emp.dat', 'W');
  8. FOR emprec IN (SELECT * FROM SO_SERVICES)
  9. LOOP
  10. LINE_BUFF :=TO_CHAR (emprec.ACCT_NO) ;
  11. UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
  12. END LOOP;
  13. UTL_FILE.FCLOSE (fileID);
  14. END;
  15.  
  16. /
  17.  
ARE

R at line 2:
00901: invalid CREATE command

Please enclose your source code in [code] tags for better readability
Mar 12 '08 #5
amitpatel66
2,367 Expert 2GB
Try something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> CREATE OR REPLACE DIRECTORY LOGG_DIR AS 'C:\TEST';
  3.  
  4. SQL>DECLARE
  5. fileID UTL_FILE.FILE_TYPE;
  6. LINE_BUFF VARCHAR2(1000);
  7. BEGIN
  8. fileID := UTL_FILE.FOPEN (LOGG_DIR, 'emp.dat', 'W');
  9. FOR emprec IN (SELECT * FROM SO_SERVICES)
  10. LOOP
  11. LINE_BUFF :=TO_CHAR (emprec.ACCT_NO) ;
  12. UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
  13. END LOOP;
  14. UTL_FILE.FCLOSE (fileID);
  15. END;
  16.  /
  17.  
Mar 12 '08 #6
amitpatel66
2,367 Expert 2GB
Duplicate Threads Merged for better management of the forum

MODERATOR
Mar 12 '08 #7
change
26
RAN IT AND THE ERRORS ARE AT BOTTOM
Expand|Select|Wrap|Line Numbers
  1.  
  2. 1 DECLARE
  3. 2 fileID UTL_FILE.FILE_TYPE;
  4. 3 LINE_BUFF VARCHAR2(1000);
  5. 4 BEGIN
  6. 5 fileID := UTL_FILE.FOPEN ('C:\TEST', 'emp.dat
  7. 6 FOR emprec IN (SELECT * FROM SO_SERVICES)
  8. 7 LOOP
  9. 8 LINE_BUFF :=TO_CHAR (emprec.ACCT_NO) ;
  10. 9 UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
  11. 10 END LOOP;
  12. 11 UTL_FILE.FCLOSE (fileID);
  13. 12* END;
  14. SQL> /
  15. DECLARE
  16. *
  17. ERROR at line 1:
  18. ORA-06510: PL/SQL: unhandled user-defined exception
  19. ORA-06512: at "SYS.UTL_FILE", line 120
  20. ORA-06512: at "SYS.UTL_FILE", line 204
  21. ORA-06512: at line 5
  22.  
  23.  
Mar 12 '08 #8
amitpatel66
2,367 Expert 2GB
Try This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. fileID UTL_FILE.FILE_TYPE;
  4.  LINE_BUFF VARCHAR2(1000);
  5.  BEGIN
  6.  fileID := UTL_FILE.FOPEN ('C:\TEST', 'emp.dat
  7.  FOR emprec IN (SELECT * FROM SO_SERVICES)
  8.  LOOP
  9.  LINE_BUFF :=TO_CHAR (emprec.ACCT_NO) ;
  10.  UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
  11.  END LOOP;
  12. UTL_FILE.FCLOSE (fileID);
  13. EXCEPTION
  14. WHEN OTHERS THEN
  15. DBMS_OUTPUT.PUT_LINE('Errrrrrrr');
  16. END;
  17.  
  18.  
Mar 12 '08 #9
change
26
get :PL/SQL procedure successfully completed but the data aint there in the specified file.
Mar 12 '08 #10
amitpatel66
2,367 Expert 2GB
get :PL/SQL procedure successfully completed but the data aint there in the specified file.
Do this and let me know does it print 'Errrrrr':

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> SET SERVEROUTPUT ON
  3.  
  4.  
THen run that piece of code
Mar 12 '08 #11
amitpatel66
2,367 Expert 2GB
Do this and let me know does it print 'Errrrrr':

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> SET SERVEROUTPUT ON
  3.  
  4.  
THen run that piece of code
Do the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SQL> CREATE OR REPLACE DIRECTORY LOGG_DIR AS 'C:\TEST';
  3.  
  4. SQL> GRANT READ,WRITE ON DIRECTORY LOGG_DIR TO PUBLIC;
  5.  
  6. SQL> SET SERVEROUTPUT ON
  7.  
  8. SQL> <The piece of Code that I have given in my previous Post>
  9.  
  10.  
Post back what happens when you do these steps?
Mar 12 '08 #12
change
26
Did As U Said And Printed Errrrrrrrrrr
Mar 12 '08 #13
change
26
Can U Get A Dummy One Working For Me Man Am In Need
Mar 12 '08 #14
amitpatel66
2,367 Expert 2GB
To get the exact error message, please run this code and post back the error message for reference

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. fileID UTL_FILE.FILE_TYPE;
  4.  LINE_BUFF VARCHAR2(1000);
  5.  BEGIN
  6.  fileID := UTL_FILE.FOPEN ('C:\TEST', 'emp.dat
  7. FOR emprec IN (SELECT * FROM SO_SERVICES)
  8. LOOP
  9. LINE_BUFF :=TO_CHAR (emprec.ACCT_NO) ;
  10. UTL_FILE.PUT_LINE ( FILEID ,LINE_BUFF);
  11. END LOOP;
  12. UTL_FILE.FCLOSE (fileID);
  13. EXCEPTION
  14. WHEN OTHERS THEN
  15. DBMS_OUTPUT.PUT_LINE(SQLCODE||','||SQLERRM);
  16. END;
  17.  
  18.  
Mar 12 '08 #15
amitpatel66
2,367 Expert 2GB
You can search in this forum by typing in the keyword utl_file in the SEARCH box at the top right corner of the forum windowto get some sample codes.

But you run the code that I have provided you in my previous post to check what could be the exact error and post back the error for reference.
Mar 12 '08 #16
change
26
HI RAN THE CODE AND THIS IS THE ERROR;;;


1,User-Defined Exception
Mar 13 '08 #17
change
26
HI WAS DOING SOME REDING AND CAME ACROOS::::

Here's the format of the parameter for file access in the INIT.ORA file:

utl_file_dir = <directory>


PROBLEM IS WHERE DO I DECLARE THIS IN MY CODE
Mar 13 '08 #18
change
26
scribes

am trying to use utl_file and need to declare the directory in init.ora and i went to the oracle registry folder and i cant seem to find it....help please
Mar 13 '08 #19
amitpatel66
2,367 Expert 2GB
HI WAS DOING SOME REDING AND CAME ACROOS::::

Here's the format of the parameter for file access in the INIT.ORA file:

utl_file_dir = <directory>


PROBLEM IS WHERE DO I DECLARE THIS IN MY CODE
If you want to change the value of utl_file_dir, then you need to change that init.ora file and bounce the database server to take in to effect the modified value
Mar 13 '08 #20
amitpatel66
2,367 Expert 2GB
scribes

am trying to use utl_file and need to declare the directory in init.ora and i went to the oracle registry folder and i cant seem to find it....help please
You need to assign the directory value to utl_file_dir parameter in init.ora file. The init .ora file is present in ORACLE_HOME/dbs path. Check for INIT.ORA File, assign the directory value to the utl_file_dir parameter and bounce the DB server. You can ask your DBA to do this for you.
Mar 13 '08 #21
amitpatel66
2,367 Expert 2GB
Duplicate Threads merged for better management of forum


MODERATOR
Mar 13 '08 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Igor Shulgin | last post by:
Hi! What is standard and direct way (within Oracle 9.2 stored procedures) for writing binary data from Oracle to external file on disk? We have...
5
by: Siddhant Kumar | last post by:
I want to write a Text file containing data (either , or Tab delimited) using stored procedure. can any one guide me how to do this.? or point to a...
1
by: Leo Martin Orfei | last post by:
Hi, Anybody know/have functions or package to create text file from postgres function? I want to execute a function to take some fields from...
1
by: tim | last post by:
Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi...
2
by: dkarthick | last post by:
How to execute UTL_FILE Package in plsql?
1
by: anithas | last post by:
I need to write the o/p from an Oracle Pl/SQL to a txt file. I tried the following , but i get an error declare f utl_file.file_type; s...
1
by: djramesh04 | last post by:
ORA-29280: invalid directory path ORA-06512: at "SYS.UTL_FILE", line 33 ORA-06512: at "SYS.UTL_FILE", line 436 ORA-06512: at...
2
by: murthydb2 | last post by:
Hi My requirement is that i have to write a stored procedure in db2 and that will be executed in a batch file . Any system error or...
5
by: narendrareddy | last post by:
Hi , assume a flat file name emp.txt contains the following data 10000100001 narendranathreddy tallapireddy 01-01-2007 male 10000100002...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.