Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directly export a query to CSV using SQL Developer

Tags:

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.

like image 788
olovholm Avatar asked Oct 18 '13 11:10

olovholm


People also ask

How do I export data from a query in Oracle SQL Developer?

SQL Developer provides the ability to export user data to a variety of formats: CSV, XML, LOADER, TEXT, INSERT, HTML and XLS. In order to export the data from a table you can either use the SQL Worksheet and write a SQL query to retrieve the required data or you can Click on the Data tab of a table definition.

How do I save a query in SQL Developer?

You can save them as files in your hard disk. Try closing those query views and SQL-Developer will prompt you to save them (or not). Or choose Save from the File menu, like...

How do I export selected data from SQL Developer?

To start, you'll need to run your query in SQL Developer. ... Step 2: Open the Export Wizard. ... Step 3: Select the Excel format and the location to export your file. ... Step 4: Export the query output to Excel.


1 Answers

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.

like image 189
Alex Poole Avatar answered Oct 14 '22 10:10

Alex Poole