Exporting results of a query in SQL developer without having to execute the query twice

oracle-sql-developer

Is there anyway to export the results of a query in Oracle SQL Developer without having to execute the query twice? Running a SQL query in worksheet, then Right click on Result Set window -> Export Data -> Text will run the query a second time.

Best Answer

There are some SQL Developer specific comments/"hints". For example if you run the below as a script (F5) and not a statement (Ctrl-Enter):

select /*csv*/ * from table;

You will get the results in CSV format the first time already. You can even spool the output just as in SQL*Plus. So you could just run the below block of code as a script (select lines and F5) and get a CSV directly in one pass:

spool C:\Users\XYZ\Desktop\my.csv
select /*csv*/ * from table;
spool off

Starting with version 4.1, you do not even need to use the above comment/"hint". You can just:

SET SQLFORMAT csv

Then run your query as a script.

Further options here: http://www.thatjeffsmith.com/archive/2012/05/formatting-query-results-to-csv-in-oracle-sql-developer/