How to write to a CSV file using Oracle SQL*Plus
Posted by: AJ Welch
[SQLPlus](https://docs.oracle.com/cd/B19306_01/server.102/b14357/qstart.htm) is an interactive tool that comes installed with every modern Oracle installation. SQLPlus allows you to go beyond the standard database queries with batches, scripts, and calculations beyond the normal scope of Oracle.
It is even possible, as we’ll explore below – to generate text files, such as .csv
, using the output of a particular database query.
Launching SQL*Plus
Depending on your Oracle installation, you may have access to one of many different versions or “modes” in which to run the SQL*Plus application.
SQL*Plus Command-line
If you wish to use SQL*Plus Command-line
, you’ll simply issue the sqlplus
command from your shell:
$ sqlplus
This will attempt to connect you to the default database and you’ll be prompted to enter your credentials to authenticate yourself.
In the event you need to connect to a different database
or use a different user
(schema
), try the following, replacing your own values as necessary:
$ sqlplus schema@//machine.domain:port/database
iSQL*Plus
If you have access to it, you may wish to use iSQL*Plus
, which is a browser-based version of the SQL*Plus command-line tool.
This is accomplished by simply visiting the iSQL*Plus URL
for your database and installation. The exact URL will vary, but it is typically in the following format: http://machine_name.domain:port/isqlplus
SQL*Plus for Windows
If you’re using Windows, there is also a Windows GUI version of SQL*Plus, which can typically be launched from your start menu: Start > Programs > Oracle > Application Development > SQL Plus
.
Outputting a query to a file
Now that you’re connected to SQL*Plus we can begin creating our file.
Modify SQL*Plus configuration
The first step is to configure some SQL*PLus system settings using the SET statement.
For this example, we’ll be manually altering these settings one time prior to our query and file generation, but if desired, you can change the defaults of various settings in your User Profile, located in the login.sql
file.
set colsep ,
set headsep off
set pagesize 0
set trimspool on
The first few settings you typically won’t want to change, but we’ll briefly explain what each accomplishes.
colsep
is the separator character used to split your columns. For a.csv
file, this is a simple comma.headsep
is the separator character for the header row (if you require one). In this example we’re not outputting the header row, so we’ll leave thisoff
.pagesize
is the number of lines “per page.” This is a slightly archaic setting that is intended for printing without having too many lines per page. With a value of0
, we don’t use pages since we’re outputting to a file. If you elect to show theheader
row, setpagesize
to a very large number (larger than the expected number of record results in the query), so your header row will only show up one time rather than once “per page.”trimspool
set toon
simply removes trailing whitespace.
Now the final two settings will need to be altered depending on your query.
set linesize #
set numwidth #
- For
linesize
, the#
value should be the total number of output columns in your resulting query. numwidth
is the column width (number of character spaces) used when outputting numeric values.
SPOOL command
Note: The SPOOL
command is unavailable in the browser-based SQL*Plus version, iSQL*Plus
. To generate files while using iSQL*Plus, change the necessary preference settings to directly output to a file.
With our settings taken care of, now we must tell SQL*Plus to output a file. This is accomplished using the SPOOL statement.
While SPOOL
is active, SQL*PLus will store the output of any query to the specified file.
Therefore, the next command to enter is spool
:
spool file_path
Skipping ahead slightly, after your query is inserted, you also need to halt spool
so the file output is closed by using the spool off
command:
spool off
Insert the query
The last step after the settings are modified and spool
is running is to insert your query. For our simple example, we’re outputting all books from our books
table.
SELECT
title,
primary_author
FROM
books;
Don’t forget the semi-colon to close out your query statement, then enter the aforementioned spool off
command.
That’s it, you’ve generated a new text file with the results of your query using SQL*Plus.
Tip: Using a script file
Rather than manually entering every line, it is suggested to enter all the settings into a new script file that you can execute in SQL*Plus in a single command.
Create a new script file with the EDIT
statement:
EDIT file_name
Now paste the entire script command list into your new file and save. The full contents of our example script can be found below.
set colsep ,
set headsep off
set pagesize 0
set trimspool on
set linesize 2
set numwidth 5
spool books.csv
SELECT
title,
primary_author
FROM
books;
spool off
To execute the script, simply use the @
symbol followed by the file name:
@file_name
Your script should be executed and the .csv
file created as expected.