How to Export Query Results to a CSV File in SQLcl (Oracle)

If you’re using SQLcl to run queries against Oracle Database, then you can use the SPOOL command to export your query results to a CSV file.

Example

Here’s an example that exports a whole table:

SET SQLFORMAT csv
SPOOL '/Users/barney/data/countries.csv';
SELECT * FROM countries;
SPOOL off;
SET SQLFORMAT ansiconsole

Here’s what it did, line by line:

  1. My first line sets the SQLFORMAT to csv. This ensures that the output is actually comma separated. If I didn’t do this, I could end up with a file with a .csv extension, but with content that’s not comma separated.
  2. The second line is where we use the SPOOL command to specify where the output file will be written. Be sure to change the /Users/barney/data/countries.csv part to a location on your system.
  3. On the third line, I ran the SQL query – the results for which I’m exporting. In this case, I exported the whole countries table.
  4. Next I turned SPOOL off.
  5. Finally, I finished by setting the SQLFORMAT back to ansiconsole (which was what I was using before setting it to csv).

Here’s what the resulting file looks like:

"COUNTRY_ID","COUNTRY_NAME","REGION_ID"
"AR","Argentina",2
"AU","Australia",3
"BE","Belgium",1
"BR","Brazil",2
"CA","Canada",2
"CH","Switzerland",1
"CN","China",3
"DE","Germany",1
"DK","Denmark",1
"EG","Egypt",4
"FR","France",1
"HK","HongKong",3
"IL","Israel",4
"IN","India",3
"IT","Italy",1
"JP","Japan",3
"KW","Kuwait",4
"MX","Mexico",2
"NG","Nigeria",4
"NL","Netherlands",1
"SG","Singapore",3
"UK","United Kingdom",1
"US","United States of America",2
"ZM","Zambia",4
"ZW","Zimbabwe",4

25 rows selected. 

Remove Column Headers

You can remove the column headers with SET HEADING off:

SET SQLFORMAT csv
SET HEADING off
SPOOL '/Users/barney/data/countries.csv';
SELECT * FROM countries;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on

Result:

"AR","Argentina",2
"AU","Australia",3
"BE","Belgium",1
"BR","Brazil",2
"CA","Canada",2
"CH","Switzerland",1
"CN","China",3
"DE","Germany",1
"DK","Denmark",1
"EG","Egypt",4
"FR","France",1
"HK","HongKong",3
"IL","Israel",4
"IN","India",3
"IT","Italy",1
"JP","Japan",3
"KW","Kuwait",4
"MX","Mexico",2
"NG","Nigeria",4
"NL","Netherlands",1
"SG","Singapore",3
"UK","United Kingdom",1
"US","United States of America",2
"ZM","Zambia",4
"ZW","Zimbabwe",4

25 rows selected. 

In this case, I turned HEADINGS back on after I exported the file.

Remove Feedback

You can remove the X rows selected with SET FEEDBACK off:

SET SQLFORMAT csv
SET HEADING off
SET FEEDBACK off
SPOOL '/Users/barney/data/countries.csv';
SELECT * FROM countries;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 
SET FEEDBACK on

Result:

"AR","Argentina",2
"AU","Australia",3
"BE","Belgium",1
"BR","Brazil",2
"CA","Canada",2
"CH","Switzerland",1
"CN","China",3
"DE","Germany",1
"DK","Denmark",1
"EG","Egypt",4
"FR","France",1
"HK","HongKong",3
"IL","Israel",4
"IN","India",3
"IT","Italy",1
"JP","Japan",3
"KW","Kuwait",4
"MX","Mexico",2
"NG","Nigeria",4
"NL","Netherlands",1
"SG","Singapore",3
"UK","United Kingdom",1
"US","United States of America",2
"ZM","Zambia",4
"ZW","Zimbabwe",4

Here, I turned FEEDBACK back on after exporting the file.

Multiple Tables

In this example, I export the results of a slightly more complex query that joins two tables:

SET SQLFORMAT csv 
SET HEADING off 
SET FEEDBACK off
SPOOL '/Users/barney/data/employees_jobs.csv';
SELECT 
    e.employee_id,
    e.first_name,
    e.last_name,
    e.salary,
    j.job_title
FROM employees e
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.salary BETWEEN 12000 AND 15000
ORDER BY SALARY DESC;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 
SET FEEDBACK on

Resulting file:

145,"John","Russell",14000,"Sales Manager"
146,"Karen","Partners",13500,"Sales Manager"
201,"Michael","Hartstein",13000,"Marketing Manager"
147,"Alberto","Errazuriz",12000,"Sales Manager"
205,"Shelley","Higgins",12000,"Accounting Manager"
108,"Nancy","Greenberg",12000,"Finance Manager"

Append the Results

By default, SPOOL uses REPLACE, which replaces the file if it already exists.

However, we can use the APPEND argument to append the results to the file.

SET SQLFORMAT csv 
SET HEADING off 
SET FEEDBACK off
SPOOL '/Users/barney/data/employees_jobs.csv' APPEND;
SELECT 
    e.employee_id,
    e.first_name,
    e.last_name,
    e.salary,
    j.job_title
FROM employees e
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.salary BETWEEN 11000 AND 11999
ORDER BY SALARY DESC;
SPOOL off;
SET SQLFORMAT ansiconsole
SET HEADING on 
SET FEEDBACK on

Resulting file:

145,"John","Russell",14000,"Sales Manager"
146,"Karen","Partners",13500,"Sales Manager"
201,"Michael","Hartstein",13000,"Marketing Manager"
147,"Alberto","Errazuriz",12000,"Sales Manager"
205,"Shelley","Higgins",12000,"Accounting Manager"
108,"Nancy","Greenberg",12000,"Finance Manager"
168,"Lisa","Ozer",11500,"Sales Representative"
174,"Ellen","Abel",11000,"Sales Representative"
114,"Den","Raphaely",11000,"Purchasing Manager"
148,"Gerald","Cambrault",11000,"Sales Manager"

This example appended the results to the file that was created (and populated) in the previous example.