270

I'm using Oracle SQL Developer 3.0. Trying to figure out how to export a query result to a text file (preferably CSV). Right clicking on the query results window doesn't give me any export options.

1

6 Answers 6

416

Version I am using

Oracle SQL Developer Version 3.0.02; Build MAIN-02.37

Update 5th May 2012

Jeff Smith has blogged showing, what I believe is the superior method to get CSV output from SQL Developer. Jeff's method is shown as Method 1 below:

Method 1

Add the comment /*csv*/ to your SQL query and run the query as a script (using F5 or the 2nd execution button on the worksheet toolbar)

select /*csv*/ *
from emp;

Screenshot of SQL developer executing the SQL statement above as a script showing the output automatically formatted as valid CSV.

That's it.

You can also use spool to automatically save it as a CSV file:

spool "/path/to/file.csv";
select /*csv*/ *
from emp;
spool off;

Just be sure to "Run as Script" or press F5.

Method 2

Run a query

alt text

Right click and select unload.

Update. In Sql Developer Version 3.0.04 unload has been changed to export Thanks to Janis Peisenieks for pointing this out

alt text

Revised screen shot for SQL Developer Version 3.0.04

enter image description here

From the format drop down select CSV

alt text

And follow the rest of the on screen instructions.

Sign up to request clarification or add additional context in comments.

14 Comments

+1. The term "Unload" seems anti-intuitive to me. To "Load" some data means to insert it into the database; therefore "Unload" should mean the data is deleted...
Also note method 2 can run into a bug (still present in my version 3.0.04) where it just hangs with large row count (3K rows or so). I'm using this old school SQLPLUS cause I didn't see method 1 above first, but some may like this: {code} SET UNDERLINE OFF SET COLSEP ',' SET LINES 20000 PAGES 20000 SET FEEDBACK off --optional SET HEADING off Spool C:\Export\EMP.csv --Now the query SELECT * FROM EMP; Spool OFF
@topr Use method 1, then select all, copy and paste into a text editor and save as csv. You might even be able to paste directly into Excel, but I'm not sure about that.
From your comment it looks like there is no space after the end of the comment. What happens when you try the following. SELECT /*csv*/ * FROM employee;
@half-pass I don't have access to 4.02. In 4.03 it works as described in method 2. The /*csv*/ method doesn't create a file. You copy the results from the script output tab and paste into the target file\app
|
53

Not exactly "exporting," but you can select the rows (or Ctrl-A to select all of them) in the grid you'd like to export, and then copy with Ctrl-C.

The default is tab-delimited. You can paste that into Excel or some other editor and manipulate the delimiters all you like.

Also, if you use Ctrl-Shift-C instead of Ctrl-C, you'll also copy the column headers.

6 Comments

awesome, i was looking specifically on how to copy headers. thanks!
The only issue, if you have a lot of rows, this will mean it will have to query the entire dataset again. and if it's a long running query, this means you wait a lot for the first page, and then wait a lot for all pages after pressing ctrl+A. in other words, great solution, but works only most of the time, and for relatively faster or smaller queries.
On long row count it will strip your copied stuff! Do not use this.
@Mark Never seen that happen myself. Did it only keep the first 50 rows or something like that? If so there's a separate preference for that. It only copies what's currently in the grid, but if you scroll to the bottom of the grid SQL Dev will automatically fetch more rows, so I can see how someone might interpret that as a bug.
I tried doing this on 43K rows of data and it just runs forever--export option way quicker. This is awesome if you have just a few rows of data.
|
31

FYI, you can substitute the /*csv*/ for other formats as well including /*xml*/ and /*html*/. select /*xml*/ * from emp would return an xml document with the query results for example. I came across this article while looking for an easy way to return xml from a query.

6 Comments

/*insert*/ is particularly useful.
Where do the files go?
Ah never mind, I glossed over the "run as script" part, not realizing this was distinct from the usual run button.
@Anomaly So, where do the files go? Thanks.
@Anomaly No problem. From other answers and comments I learned that you specify a file path like so SPOOL 'file01.csv' REPLACE; SELECT * FROM foo WHERE categ in ('A', 'B'); SPOOL off;
|
6

FYI to anyone who runs into problems, there is a bug in CSV timestamp export that I just spent a few hours working around. Some fields I needed to export were of type timestamp. It appears the CSV export option even in the current version (3.0.04 as of this posting) fails to put the grouping symbols around timestamps. Very frustrating since spaces in the timestamps broke my import. The best workaround I found was to write my query with a TO_CHAR() on all my timestamps, which yields the correct output, albeit with a little more work. I hope this saves someone some time or gets Oracle on the ball with their next release.

3 Comments

My version 3.0.04 still has a bug where it just hangs with larger exports (mine is 3K rows). My simple fix was to use SQLPLUS instead:
In my case the Spatial Data Type (docs.oracle.com/cd/B19306_01/appdev.102/b14255/…) were being exported like this (notice the commas) MDSYS.SDO_GEOMETRY(2001,8307,MDSYS.SDO_POINT_TYPE(-122.39096,37.79251,NULL),NULL,NULL) without being wrapped in quotes. And I'm using dynamic SQL so I can't TO_CHAR() these columns. Any suggestions?
Even though I'm using Dynamic SQL and just selecting *, (and I don't want to modify this for an exception for a single SQL column of Spatial datatypes), maybe I could try two things: 1) Use a different COLSEP like '|', or 2) Before the SELECT statement, specify how this column should be formatted using docs.oracle.com/cd/B19306_01/server.102/b14357/ch12013.htm
4

To take an export to your local system from sql developer.

Path : C:\Source_Table_Extract\des_loan_due_dtls_src_boaf.csv

    SPOOL "Path where you want to save the file"
    SELECT /*csv*/ * FROM TABLE_NAME;

Comments

1

CSV Export does not escape your data. Watch out for strings which end in \ because the resulting \" will look like an escaped " and not a \. Then you have the wrong number of " and your entire row is broken.

3 Comments

That's probably not a bug - you can decide how quotes are escaped, and the default is to escape it with another quote character, not a backslash. In that case, "foo\" is a perfectly valid quoted string.
It's simple enough to replace every occurence of \ with \\, if you know about it in advance. Thanks!
Yes, simple enough and likely wrong for most CSV import tools.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.