You can spool the output of an Oracle SQL query to a file using SQLPlus or tools like SQL Developer that support SQLPlus commands.
Here's a sample script to spool the employees table output to a text file, followed by an example for a CSV file.
Method 1: Spooling to a Plain Text File
This will create a text file with the default column formatting (aligned, with headers).
-- 1. Set SQL*Plus environment variables
SET ECHO OFF; -- Don't echo commands in the spool file
SET FEEDBACK OFF; -- Don't display "X rows selected" messages
SET TERMOUT OFF; -- Don't display output on screen while spooling (useful for large files)
SET PAGESIZE 0; -- No page breaks or repeated headers
SET LINESIZE 32767; -- Max line size to prevent wrapping (adjust if needed for extremely wide tables)
-- 2. Start spooling to a file
-- Specify the full path if you don't want it in the current directory.
SPOOL employees_output.txt
-- 3. Your SQL query
SELECT *
FROM employees;
-- 4. Stop spooling
SPOOL OFF;
-- 5. Restore SQL*Plus default settings (optional, but good practice if running interactively)
SET PAGESIZE 14; -- Restore default page size
SET FEEDBACK ON; -- Restore feedback messages
SET TERMOUT ON; -- Restore terminal output
SET ECHO ON; -- Restore echo commands
How to run this:
- Save the above code as a
.sql file (e.g., spool_employees.sql).
- Open SQL*Plus or SQL Developer.
- Connect to your Oracle database.
- Execute the script using
@ command:
@spool_employees.sql
- The
employees_output.txt file will be created in the directory where you ran the SQL*Plus command, or the path you specified.
Method 2: Spooling to a CSV (Comma Separated Values) File
This is often more useful for importing into spreadsheets or other applications.
-- 1. Set SQL*Plus environment variables for CSV output
SET ECHO OFF;
SET FEEDBACK OFF;
SET TERMOUT OFF;
SET PAGESIZE 0;
SET HEADING OFF; -- Don't show column headers (we'll manually add them)
SET LINESIZE 32767; -- Max line size
-- 2. Start spooling
SPOOL employees_data.csv
-- 3. Add the header row (optional, but good for CSV)
SELECT 'EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,COMMISSION_PCT,MANAGER_ID,DEPARTMENT_ID'
FROM DUAL;
-- 4. Your SQL query for CSV
-- Concatenate columns with commas. Use NVL for NULLs if they should be empty strings.
-- Use TO_CHAR for dates/numbers to control format (especially for dates).
-- Use CHR(34) to enclose string values in double quotes if they might contain commas
-- (this is robust CSV generation, though more complex).
-- For simplicity, let's just concatenate directly, assuming no embedded commas in string fields.
SELECT
e.employee_id || ',' ||
e.first_name || ',' ||
e.last_name || ',' ||
e.email || ',' ||
e.phone_number || ',' ||
TO_CHAR(e.hire_date, 'YYYY-MM-DD') || ',' || -- Format date as YYYY-MM-DD
e.job_id || ',' ||
e.salary || ',' ||
NVL(TO_CHAR(e.commission_pct), '') || ',' || -- Handle potential NULL for numbers
NVL(TO_CHAR(e.manager_id), '') || ',' ||
NVL(TO_CHAR(e.department_id), '')
FROM
employees e;
-- 5. Stop spooling
SPOOL OFF;
-- 6. Restore SQL*Plus default settings
SET PAGESIZE 14;
SET FEEDBACK ON;
SET TERMOUT ON;
SET HEADING ON;
SET ECHO ON;
Important considerations for CSV:
- Headers: I've added a
SELECT '...' FROM DUAL; for the header row. Alternatively, you could use SET HEADING ON; SET COLSEP ','; and select the columns, but manually concatenating offers more control over formatting and handling special characters/nulls.
- Quotes: For robust CSV, you should wrap string values in double quotes (
CHR(34) || column_name || CHR(34)) and escape any double quotes within the data (REPLACE(column_name, '"', '""')). The example above simplifies this for brevity but be aware for real-world data.
- Dates and Numbers: Always use
TO_CHAR() for dates and numbers to ensure they are formatted exactly how you want them in the CSV, especially if you have internationalization settings.
- NULLs: Use
NVL(column_name, '') to convert NULL values to empty strings in your CSV, rather than leaving them as NULL (which might appear as nothing or a specific NULL string depending on client settings).
Choose the method that best suits your needs!