The `SPOOL` command in SQL*Plus is used to direct output to a file. Below is an example of how to use it:
Example: Using `SPOOL` in SQL*Plus
-- Start SQL*Plus and connect to Oracle
sqlplus username/password@database
-- Set up the environment
SET LINESIZE 200
SET PAGESIZE 100
SET ECHO OFF
SET FEEDBACK OFF
-- Start spooling to a file
SPOOL /tmp/query_output.txt
-- Execute a query
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = 10;
-- Stop spooling
SPOOL OFF
-- Exit SQL*Plus
EXIT;
Explanation:
SET LINESIZE 200
→ Sets the line width to 200 characters to prevent text wrapping.
SET PAGESIZE 100
→ Controls the number of rows per page (helps format output).
SET ECHO OFF
→ Prevents SQL commands from being displayed in the output file.
SET FEEDBACK OFF
→ Hides query feedback like "X rows selected".
SPOOL /tmp/query_output.txt
→ Starts writing output to the specified file (/tmp/query_output.txt
on Linux/macOS or C:\output.txt
on Windows).
- Executes a
SELECT
query to fetch employee details.
SPOOL OFF
→ Stops writing output to the file.
EXIT;
→ Exits the SQL*Plus session.
This method is commonly used for exporting SQL query results to a text file for further processing or reporting.
Occasionally, you will be asked to send the query result back to the requester. If the result set fits in a page, you will be able to copy the result and send the result back. On the other hand, if the query result does not fit into a page and is greater than the size of the page you will need to the use the SPOOL command. Oracle will let you write a query result on a text file. The process of writing a query result to a file is called spooling.
Question: How you can spool a result into a file for Oracle database?
You can spool using SQL Developer or SQL PLus. Each approach is a little different even though the concept is the same.
With SQL Developer:
- Turn ON the Spool
- Run the Query
- Turn OFF the Spool
Syntax: The command below only works if you run the script using SQL Developer.
To do this, highlight all three commands, right click and run as script.
-- Turn on the spool
spool c:\users\ggould\spool_file.txt
-- Run your Query
select * from dba_tables;
-- Turn of spooling
spool off;
The SPOOL command is used to echo the output to a text file. Spooling output to a file is a good thing to do whenever you run a script, or any other command that produces a large output. It enables you to review the output later, without having to worry about it scrolling off the top of the screen. To begin writing output to a file, use the SPOOL command followed by the name of the file to which you want to write the output. You can specify the full directory path of the file if you like. The following examples will work on Windows NT and UNIX respectively:
SPOOL c:\output.txt
SPOOL $HOME/output.txt
All the output will now be written to a file named output.txt in the root directory of your C drive (Windows), or to your home directory (UNIX). To stop spooling, use the following command:
SPOOL OFF
I use the SPOOL command whenever I run a critical script, or whenever I execute a critical set of commands, because it gives me a record that I can turn to later if I find out that something did not go as expected.