ChatGPT解决这个技术问题 Extra ChatGPT

How to export query result to csv in Oracle SQL Developer?

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.


G
Greg Burghardt

Version I am using

https://i.stack.imgur.com/yZ3aQ.jpg

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;

https://i.stack.imgur.com/Iqpqw.jpg

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

https://i.stack.imgur.com/tLYlY.jpg

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

https://i.stack.imgur.com/CQ4Qb.jpg

Revised screen shot for SQL Developer Version 3.0.04

https://i.stack.imgur.com/BfgNB.jpg

From the format drop down select CSV

https://i.stack.imgur.com/tsWqP.jpg

And follow the rest of the on screen instructions.


+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...
In version 3.0.04 this option has been renamed to Export.
Great. p.s. upper case /*CSV*/ didn't work for me, but lower case /*csv*/ did. thanks!
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.
H
Hulk1991

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.


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.
Y
Yves Rochon

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.


/*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;
B
BoeroBoy

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.


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
A
Arpan Saini

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;

A
Anonymous

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.


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.