How do I spool to a CSV formatted file using SQLPLUS?
You could also use the following, although it does introduce spaces between fields.
set colsep , -- separate columns with a commaset pagesize 0 -- No header rowsset trimspool on -- remove trailing blanksset headsep off -- this may or may not be useful...depends on your headings.set linesize X -- X should be the sum of the column widthsset numw X -- X should be the length you want for numbers (avoid scientific notation on IDs)spool myfile.csvselect table_name, tablespace_name from all_tables where owner = 'SYS' and tablespace_name is not null;
Output will be like:
TABLE_PRIVILEGE_MAP ,SYSTEM SYSTEM_PRIVILEGE_MAP ,SYSTEM STMT_AUDIT_OPTION_MAP ,SYSTEM DUAL ,SYSTEM ...
This would be a lot less tedious than typing out all of the fields and concatenating them with the commas. You could follow up with a simple sed script to remove whitespace that appears before a comma, if you wanted.
Something like this might work...(my sed skills are very rusty, so this will likely need work)
sed 's/\s+,/,/' myfile.csv
I use this command for scripts which extracts data for dimensional tables (DW). So, I use the following syntax:
set colsep '|'set echo offset feedback offset linesize 1000set pagesize 0set sqlprompt ''set trimspool onset headsep offspool output.datselect '|', <table>.*, '|' from <table>where <conditions>spool off
And works. I don't use sed for format the output file.