Exporting a table to CSV in Oracle means using SQL*Plus formatting commands and SPOOL to write query results to a comma-separated file.
Open SQL*Plus, apply formatting commands, start SPOOL
, run a SELECT
, then stop spooling. The resulting file is a clean CSV ready for Excel or any ETL job.
Use SET COLSEP ','
for commas, SET PAGESIZE 0
and SET FEEDBACK OFF
to remove blank lines, SET HEADING ON
for column names, and SET TERMOUT OFF
to hide screen output while spooling.
-- export_products.sql
SET COLSEP ','
SET PAGESIZE 0
SET FEEDBACK OFF
SET TERMOUT OFF
SPOOL /tmp/products.csv
SELECT id,
name,
price,
stock
FROM products
ORDER BY id;
SPOOL OFF
EXIT
Wrap sensitive columns in double quotes: SELECT '"' || name || '"' AS name
. For dates use TO_CHAR(order_date,'YYYY-MM-DD')
so spreadsheet tools read them correctly.
Always ORDER BY
a stable key, place the file in a directory the OS user can write to, and timestamp filenames (products_&YYYYMMDD..csv
) to avoid overwriting.
Create a shell script that calls sqlplus -s user/pass@db @export_products.sql
and schedule it with cron
or Windows Task Scheduler. Pass dates or table names with substitution variables (&1
, &2
) to reuse the script.
Yes—use UTL_FILE
to open a server-side file, loop through a cursor, build comma-separated strings, and write each line. SQL*Plus is simpler for ad-hoc needs; PL/SQL suits fully automated jobs.
No. You only need read access to the table and OS write permission to the directory where the CSV is created.
Yes. Keep SET HEADING ON
(default). To exclude headers use SET HEADING OFF
.
Millions of rows are fine; SQL*Plus streams results. Disk space and network speed are the limiting factors, not SQL*Plus itself.