| Lesson 11 | The SPOOL Command |
| Objective | Capture query output and logs reliably using SQL*Plus/SQLcl SPOOL and SQL Developer script mode. |
SPOOL is a client-side command (SQL*Plus and SQLcl) that writes everything that would print to your screen into a file—query results, errors, and messages. It’s ideal for repeatable reports, audit logs, and attaching proof to change tickets.
SQL Developer supports SPOOL when you run scripts in script mode (F5). Its grid UI has separate export features that don’t use SPOOL.
-- 1) Connect, then choose a path (quote if spaces in path)
SET TERMOUT OFF -- don't echo results to screen while spooling (optional)
SET ECHO OFF -- don't echo commands into the file (set ON for debugging)
SET FEEDBACK ON -- show "n rows" footer
SET PAGESIZE 5000
SET LINESIZE 200
SET TRIMSPOOL ON -- trim trailing spaces
SPOOL /tmp/query_output.txt
\-- 2) Your work
SELECT employee\_id, first\_name, last\_name, salary
FROM hr.employees
WHERE department\_id = 10
ORDER BY last\_name;
\-- 3) Finish
SPOOL OFF
SET TERMOUT ON
-- log_run.sql
SET SERVEROUTPUT ON SIZE UNLIMITED
SET ECHO ON
SET TIMING ON
SET SQLBLANKLINES ON
SET PAGESIZE 0
SET LINESIZE 400
SET TRIMSPOOL ON
COLUMN now NEW_VALUE run_ts
SELECT TO_CHAR(SYSTIMESTAMP,'YYYY-MM-DD"T"HH24MISSFF3TZH:TZM') now FROM dual;
SPOOL logs/run\_\&run\_ts..log
PROMPT === Begin run at \&run\_ts
\-- your scripts here
@01\_prepare.sql
@02\_report.sql
@03\_cleanup.sql
PROMPT === End run at \&run\_ts
SPOOL OFF
SET PAGESIZE 0
SET LINESIZE 400
SET TRIMSPOOL ON
SPOOL /tmp/employees.txt
SELECT employee_id || '|' || last_name || '|' || salary AS line
FROM hr.employees
ORDER BY employee_id;
SPOOL OFF
SET MARKUP CSV ON DELIMITER , QUOTE ON
SET PAGESIZE 0
SPOOL /tmp/employees.csv
SELECT employee_id, first_name, last_name, salary
FROM hr.employees
ORDER BY employee_id;
SPOOL OFF
SET MARKUP CSV OFF
SET SQLFORMAT csv
SPOOL /tmp/employees.csv
SELECT employee_id, first_name, last_name, salary
FROM hr.employees;
SPOOL OFF
SET SQLFORMAT default
SET MARKUP HTML ON ENTMAP ON
SPOOL /tmp/report.html
SELECT * FROM hr.departments ORDER BY department_id;
SPOOL OFF
SET MARKUP HTML OFF
SPOOL, SET) are honored in script mode.SPOOL and has its own formats (CSV, Excel, etc.).-- visibility and formatting
SET ECHO {ON|OFF}
SET TERMOUT {ON|OFF}
SET FEEDBACK {ON|OFF}
SET HEADING {ON|OFF}
SET PAGESIZE n
SET LINESIZE n
SET TRIMSPOOL {ON|OFF}
SET COLSEP ',' -- column separator for text output
\-- file handling
SPOOL file -- overwrite or create
SPOOL APPEND file -- append if supported by client
SPOOL OFF
/tmp/out.txt, $HOME/out.txtC:\logs\out.txt or quote paths: SPOOL "C:\Users\you\My Reports\out.txt"SP2-0606 errors commonly indicate a bad path or permissions.SPOOL was started before your queries and ended with SPOOL OFF. Check TERMOUT OFF isn’t suppressing expected screen messages you rely on for validation.SET LINESIZE and use TRIMSPOOL ON. For CSV, prefer MARKUP/SQLFORMAT to avoid spacing issues.HEADING, FEEDBACK, and PAGESIZE for your target format.ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,').TO_CHAR(SYSTIMESTAMP,...)) and capture ENV/WHOAMI context in the log header if policy requires.PAGESIZE/LINESIZE/TRIMSPOOL for clean output.ECHO, TIMING, and timestamped filenames.SPOOL OFF and verify bytes written.