November 1, 2009

SQL Tip: SPOOL command

Hello everyone,

Have you ever used 'SPOOL' command in SQL? I know many books don't tell much about it but this command is very much used when you work on SQL *Plus. So here I am just sharing it with you also as yesterday only I recalled this command ;)

SPOOL command is used when we want to keep a record of our SQL commands output for later uses.

How to use it. 
SQL> SPOOL <your file name>

All SQL commands and their outputs after this command are written into the file that by default is stored in the current working directory where you invoked SQL*Plus.

Example:
SQL> SPOOL sessions.out
SQL> SELECT * FROM TAB;
............
............
SQL> SELECT * FROM EMP;
............
............
SQL> SELECT * FROM SALGRADE;
.........
.........
SQL> SPOOL OFF 

So in sessions file - all commands along with their outputs will be saved. "SPOOL OFF" will be used to stop spooling.