Skip to Main Content
  • Questions
  • How to spool each record of a sql query into different files

Breadcrumb

Connor and Chris will both be at AI World from October 12 to October 17 , the premier Oracle conference of 2025. If you're in Vegas, please come say Hi or pop into our sessions

Question and Answer

Connor McDonald

Thanks for the question, Hatim Ali.

Asked: May 22, 2017 - 6:09 am UTC

Last updated: May 22, 2017 - 8:38 am UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

I have a requirement where i need to spool reach record into a different file

Eg.

Select * from Test_Table

Output :-

ABC
XYZ
QWE
ASD
....

and so on

So need to generate individual files which will have the output saved
File1 : ABC
File2 : XYZ

How do i generate these different files.....

and Connor said...

If you're prepared to do a second pass through table, you can do:

<code>
spool unload.sql

select
'spool '||col1||chr(10)||
'select * from test_table where col1 = '''||col1||''''||chr(10)||
'spool off'
from test_table;

spool off
@unload.sql
<code>


Is this answer out of date? If it is, please let us know via a Comment