<p>I am running oracle and have a query which pulls some results from the database. I would like to write the results as a text file. How would I go about doing this?</p>

<p>My prefered way would be by using UTL_FILE. Would some one have an example of how to do this?</p>

[{'answer_id': 27569, 'author': 'diciu', 'author_id': 2811, 'author_profile': 'https://Stackoverflow.com/users/2811', 'pm_score': 1, 'selected': False, 'text': "<p>If you're running the query from <i>sqlplus</i> you can use the spool command:</p>\n\n<pre>\nspool /tmp/test.spool\n</pre>\n\n<p>After executing the spool command within a session, all output is sent to the sqlplus console as well as the /tmp/test.spool text file.</p>\n"}, {'answer_id': 27579, 'author': 'Alotor', 'author_id': 2937, 'author_profile': 'https://Stackoverflow.com/users/2937', 'pm_score': 3, 'selected': False, 'text': '<p>If you\'re using Sql Plus, is as easy as:</p>\n\n<pre>\nSQL> spool c:\\temp\\out.txt\nSQL> SELECT * FROM USERS;\nSQL> spool off\n</pre>\n\n<p>This three sentences will output the result of the query "SELECT * FROM USERS" to the file c:\\temp\\out.txt.</p>\n\n<p>You can format this query using the string manipulation functions of Oracle.</p>\n'}, {'answer_id': 27693, 'author': 'stjohnroe', 'author_id': 2985, 'author_profile': 'https://Stackoverflow.com/users/2985', 'pm_score': 3, 'selected': True, 'text': '<p>If you are using PL/SQL then you can use the UTL_FILE package, the difference from using sql+ spool is that the files are written to the server file system. UTL_FILE has a number of limitations so an alternative on the server side would be to use Java stored procedures.</p>\n'}, {'answer_id': 28238, 'author': 'stjohnroe', 'author_id': 2985, 'author_profile': 'https://Stackoverflow.com/users/2985', 'pm_score': 0, 'selected': False, 'text': '<p>This seems to be a reasonable tutorial with a few simple examples <a href="http://www.adp-gmbh.ch/ora/plsql/utl_file.html" rel="nofollow noreferrer">UTL_FILE example</a></p>\n'}, {'answer_id': 39409, 'author': 'Andrew Wood', 'author_id': 1095, 'author_profile': 'https://Stackoverflow.com/users/1095', 'pm_score': 2, 'selected': False, 'text': "<p>Use UTL_FILE in combination with CREATE DIRECTORY for ease of mapping a directory path with a name (it does not create the actual directory just a reference to it so ensure it is created first)</p>\n\n<p>example</p>\n\n<pre><code>\n  create directory logfile as 'd:\\logfile'; -- must have priv to do this\n\ndeclare\n  vFile utl_file.file_type;\nbegin\n  vFile := utl_file.fopen(logfile ,'syslog','w'); -- w is write. This returns file handle\n  utl_file.put(vFile,'Start Logfile'); -- note use of file handle vFile\n  utl_file.fclose(vFile); -- note use of file handle vFile\nend;\n\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/27562', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']