Use Spool to Export Query Results to CSV

In this short guide, you’ll see how to use Spool to export your query results to a CSV file.

You’ll also observe how to apply a quick tweak in order to export the query results to a text file.

Use Spool to Export Query Results to a CSV File

To start, here is a simple template that you may use to export your query results to CSV (note that if you’re using a specific schema, you’ll need to add that schema name before your table name):

spool 'Path where you would like to store the exported file\your_file_name.csv';
SELECT * FROM schema.table WHERE condition;
spool off;

In order to execute the Spool, you’ll need to run it as a script (for example, if you are using Oracle SQL Developer, you may press F5 to run the Spool as a script).

Your CSV file will then get created at your specified path.

But what if you want to use Spool to export the query results to a Text file?

In the next section, you’ll see how to accomplish this task.

Export Query Results to a Text File using Spool

In order to export the query results to a text file, you only need to modify the file type from csv to txt at the end of your path.

And just in case you wonder, here is a template that you may use in order to export your query results to a text file:

spool 'Path where you would like to store the exported file\your_file_name.txt';
SELECT * FROM schema.table WHERE condition;
spool off;

And as before, don’t forget to press F5 in order to run the Spool and then generate the file at your specified path.

Conclusion

You just saw how to use Spool to export query results to CSV and Text files.

Now if you’re dealing with SQL Server, you may then want to check the following guide that explains how to export query results to a text file.

You can read more about Spool by visiting the Spool Docs.

Leave a Comment