how to store output of an sql query to a text file in oracle 11g

Posted on 2011-03-04
Last Modified: 2013-12-19
how to store output of an sql query to a text file in oracle 11g
Question by:pegahelpdesk1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35037038
Copy of what I posted in your other question.  You should not ask the same question in different places.

In sqlplus use the SPOOL command.  There are a lot of sqlplus options you can set and you can actually create very nice reports.

The basic commands go like:
spool myFile.txt
select ... from table;
spool off

If you want more detailed advice on how to use sqlplus and create a specific format, ask.
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35037046
The alternative is a stored procedure using UTL_FILE.

Author Comment

ID: 35037073
thankyou, I'll try these two and get back
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 35037155
I tried the spool option and I am getting the query also in the text file, can I create a text file where only output shows up and the query used for generating the output is not written to file?
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35037208
>> tried the spool option and I am getting the query also in the text file

Are you copying and pasting the code I posted directly into sqlplus?

Don't do that.

Create a script file with those commands in it.  Say, myscript.sql

then execute the script:
sqlplus user/password @myscript.sql
LVL 14

Accepted Solution

ajexpert earned 500 total points
ID: 35044138
Here are the steps

1.  You should create a script and embed your query in it.  Just like attached file query.sql.
2.  Create script and embed the SET commands, make sure the paths are correct.  See attached file spool_query.txt
3.  navigate to directory where spool_query.txt is stored
4.  Connect to sql plus with user name and password
5.  execute the script with @spool_query.txt and check the spool file for query output


LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35095228

Can I ask why you selected the answer you did when it basically repeated everything I had already said?


Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question