Export Oracle query results to CSV with SPOOL

How SQL*Plus SPOOL and CSV markup turn Oracle query results into CSV files, what the conversion changes, where it breaks, and when another export method fits better.

Writing Oracle query results to a CSV file with SPOOL is one of the most common ways to get data out of a database and into a spreadsheet, script or another system. It needs nothing beyond a SQL*Plus or SQLcl client, but the result depends heavily on session settings. This review explains what the conversion does, what it changes, how well the output holds up when it is opened or shared, and when another approach is a better fit.

Is SPOOL to CSV suitable for your use?

SPOOL is a good fit for ad hoc exports, scheduled scripts and moderate result sets where you control the query and can check the output. Its main benefit is that it is scriptable and already available wherever SQL*Plus or SQLcl is installed. The decisive tradeoff is that SPOOL captures what the client displays, not the raw stored values. Dates, numbers, NULLs, long text and character encoding all pass through client formatting rules, so a careless setup can produce a file that looks right but has shifted dates, truncated text or broken columns.

Plain fixed-width text output is preferable when the result is only for people to read in a terminal or a log, since CSV adds quoting and delimiters that make it harder to scan by eye. For full table copies between Oracle databases, a dedicated tool such as Data Pump preserves data types and structure, which CSV cannot. For one-off exports through a graphical interface, the export wizard in SQL Developer avoids most of the session-setting pitfalls described below.

What the conversion actually changes

SPOOL does not modify anything in the database. It redirects the client's screen output to a file. The transformation happens between the stored values and that display: every value is converted to text, separated by a delimiter and, depending on the method, wrapped in quotes. CSV carries no fonts, styling or layout, so 'formatting' here means only how values are rendered as characters.

  • Dates and timestamps are rendered using the session's NLS date and timestamp formats unless the query formats them explicitly with TO_CHAR.
  • Numbers are rendered using the client's numeric formatting, which can affect decimal separators and how very large or very small values appear.
  • NULL values become empty fields, so a NULL and an empty string are indistinguishable in the file.
  • CLOB and LONG columns are cut off at the length set by SET LONG, which is small by default.
  • Characters are written in the client character set defined by NLS_LANG, which may differ from the database character set.

Method 1: CSV markup in SQL*Plus

SQL*Plus 12.2 and later support SET MARKUP CSV ON, which produces delimited output with a header row and optional quoting of values. This is generally more reliable than building lines by hand because the client handles delimiters and quoting. A typical script looks like this:

SET MARKUP CSV ON QUOTE ON
SET FEEDBACK OFF
SET ECHO OFF
SET TERMOUT OFF
SET TRIMSPOOL ON

SPOOL employees.csv

SELECT employee_id,
       last_name,
       TO_CHAR(hire_date, 'YYYY-MM-DD') AS hire_date
FROM   employees;

SPOOL OFF
SET MARKUP CSV OFF

SET FEEDBACK OFF keeps the row-count message out of the file. SET TERMOUT OFF suppresses screen output but only takes effect when commands run from a script file, not when typed interactively. A different delimiter can be set with the DELIMITER option, for example a tab or pipe character for TSV-style output. Save the commands to a file and run it in silent mode so the login banner and prompts stay out of the export:

sqlplus -s username@service @export_employees.sql

Method 2: SQLcl CSV format

SQLcl, Oracle's newer command-line client, offers SET SQLFORMAT CSV, and also accepts a /csv/ comment hint directly in a query. The SPOOL mechanics are the same. This is a convenient option if SQLcl is already available, and it behaves similarly to SQL*Plus CSV markup for most simple queries.

SET SQLFORMAT CSV
SET FEEDBACK OFF
SPOOL orders.csv
SELECT * FROM orders;
SPOOL OFF

Method 3: Manual concatenation in older clients

Older SQL*Plus versions without CSV markup require building each line in SQL by concatenating columns with a delimiter. This works, but it provides no automatic quoting, so any comma, quote or line break inside a value will break the column structure unless the query escapes it.

SET HEADING OFF
SET PAGESIZE 0
SET LINESIZE 32767
SET FEEDBACK OFF
SET TRIMSPOOL ON

SPOOL customers.csv

SELECT customer_id || ',' ||
       '"' || REPLACE(customer_name, '"', '""') || '"'
FROM   customers;

SPOOL OFF

PAGESIZE 0 suppresses headings and page breaks, so a header row, if needed, must be added with a separate SELECT or PROMPT line. LINESIZE is set to the maximum SQL*Plus allows so long rows are not wrapped, and TRIMSPOOL removes the trailing spaces that would otherwise pad every line.

Comparing the approaches

Approach | Quoting handled by client | Header row | Best suited to
SQLPlus SET MARKUP CSV | Yes, when QUOTE ON | Yes | Scripted exports on SQLPlus 12.2 or later
SQLcl SET SQLFORMAT CSV | Yes | Yes | Users already working in SQLcl
Manual concatenation | No, must be written into the query | Must be added manually | Older SQL*Plus versions
SQL Developer export wizard | Yes | Yes | One-off exports through a graphical tool

Will the file stay readable and usable after sharing?

A CSV file is plain text, so it can be opened in any text editor, read by screen readers as ordinary text, searched and edited without special software. The practical problems usually arise when the file is opened in a spreadsheet application, which may reinterpret values on import rather than show them exactly as written.

  • Identifiers with leading zeros, such as postal codes or account numbers, may lose those zeros if a spreadsheet treats them as numbers.
  • Long numeric strings may be shown in scientific notation or lose precision in some spreadsheet applications.
  • Date strings may be re-parsed according to the spreadsheet's regional settings; an unambiguous format such as YYYY-MM-DD reduces this risk.
  • Accented letters and non-Latin scripts can appear garbled if the application assumes a different encoding from the one set by NLS_LANG.
  • Embedded line breaks inside quoted values are valid CSV but are not handled consistently by every importer.

These behaviors vary by application, version and regional settings, and no specific destination was tested for this article. Before relying on an export, open a sample in the actual destination and check rows that contain commas, quotes, line breaks, leading zeros, non-ASCII characters and NULLs. If the file feeds another program rather than a person, importing it through that program's own CSV reader with explicit column types is safer than opening it directly in a spreadsheet.

Exporting a whole table

Exporting an entire table is the same process with SELECT * or an explicit column list. Listing columns is usually better because it fixes the column order and lets you apply TO_CHAR to dates and numbers. For very large tables, SPOOL writes through the client, so file size, network transfer and client settings such as ARRAYSIZE affect how long it takes; no performance measurements are available here. If the goal is to move a table between Oracle databases rather than produce a CSV, Data Pump is the more appropriate tool.

Gaps and checks before relying on the output

Details such as the exact escaping of embedded quotes, header behavior across page settings, and handling of specific data types can differ between SQL*Plus and SQLcl versions. Check the documentation for the client version in use and run a test export with representative data. Confirm that the file has the expected number of columns on every row, that dates and numbers match the source, and that no prompts, banners or row-count messages ended up in the file.