HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

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

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
withoutthetwiceexportingsqlqueryhavingdeveloperresultsexecute

Problem

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.

Solution

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/

Code Snippets

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

Context

StackExchange Database Administrators Q#117633, answer score: 7

Revisions (0)

No revisions yet.