Directly export a query to CSV using SQL Developer

olovholm picture olovholm · Oct 18, 2013 · Viewed 107.6k times · Source

Using SQL Developer to run queries works good, but I would save a lot of time if I instead of first running the query and then right click the result set and go through the export to csv routine.

I was wondering whether it is a way in SQL Developer to: 1) Write the query, and then select that the result of the query should be exported to disk. 2) Write a queue of several queries, each of them writing their results to disk.

Answer

Alex Poole picture Alex Poole · Oct 18, 2013

You can use the spool command (SQL*Plus documentation, but one of many such commands SQL Developer also supports) to write results straight to disk. Each spool can change the file that's being written to, so you can have several queries writing to different files just by putting spool commands between them:

spool "\path\to\spool1.txt"

select /*csv*/ * from employees;

spool "\path\to\spool2.txt"

select /*csv*/ * from locations;

spool off;

You'd need to run this as a script (F5, or the second button on the command bar above the SQL Worksheet). You might also want to explore some of the formatting options and the set command, though some of those do not translate to SQL Developer.

Since you mentioned CSV in the title I've included a SQL Developer-specific hint that does that formatting for you.

A downside though is that SQL Developer includes the query in the spool file, which you can avoid by having the commands and queries in a script file that you then run as a script.