Muhammad Nadeem Chaudhry's Blog


Spool to .csv file

Posted in Uncategorized by Muhammad Nadeem Chaudhry on January 11, 2011

Sometimes you need to export result of the query to CSV file. Here is a nice example of how to do this kind of operations.

SQL> SET LINESIZE 500 FEEDBACK OFF TRIMSPOOL ON TERMOUT OFF HEAD OFF PAGESIZE 0 TERM OFF

SQL> spool outfile.csv

SQL> select ‘”‘|| column_1 || ‘”,’ || column_2 || ‘,’ || column_3 from mytable where [your where statement]

SQL> spool off

SQL> exit

In this case column_1 data will be surrounded by “” (you may need this if you have , character in field data).

Update: SET LINESIZE determines a maximum number of characters in the line. So if you expect output bigger than 500 lines you must set this to a bigger value.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: