title: Spool CSV file using BASH Script | Errors with Output in generated CSV
body: <p>I need to spool the output of tables from db to <strong>CSV</strong> file.
  I checked and referred to <a href="https://stackoverflow.com/questions/643137/how-do-i-spool-to-a-csv-formatted-file-using-sqlplus"><strong>How do I spool to a CSV formatted file using SQLPLUS?</strong></a></p>
  
  <p>But it's not working successfully. What I am getting is only the first 3 columns being separated by <code>,</code> and not the remaining. Others are getting separated by newline.</p>
  
  <p><strong>EDIT : Schema Details of Table being Spooled.</strong></p>
  
  <pre><code>WWID               VARCHAR2 (5 Byte) 
  TIMELOG_DATE       DATE 
  TOTAL_HOURS        NUMBER (10,2) 
  ACTIVITY           VARCHAR2 (100 Byte) 
  SUBACTIVITY        VARCHAR2 (100 Byte) 
  REF_PROJECT_ID     VARCHAR2 (30 Byte) 
  REF_PROJECT_DESC   VARCHAR2 (250 Byte)
  WORK_REQUEST_ID    VARCHAR2 (30 Byte)
  EMP_COMMENTS       VARCHAR2 (100 Byte)
  APPROVER_COMMENTS  VARCHAR2 (100 Byte)
  </code></pre>
  
  <p><strong>Script:</strong></p>
  
  <pre><code>echo "\n&gt;&gt;&gt; ******Data Processing Started at `date '+%d-%m-%Y %T %Z'`******" &gt;&gt;${LOGFILE}
  
  sqlplus $fims_user/$fims_pwd &lt;&lt; EOF
  set serveroutput on;
  set colsep ,     ;-- separate columns with a comma
  set pagesize 0   ;-- No header rows
  set trimspool on ;-- remove trailing blanks
  set headsep off  ;-- this may be useful...depends on your headings.
  
  spool /home/fimsctl/datafiles/outbound/timelog/timelog_file_`date +%y%m%d`.csv
  
  select * from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;
  
  commit;
  spool off
  exit
  EOF
  
  echo "\n&gt;&gt;&gt; ******Data Load Completed at `date '+%d-%m-%Y %T %Z'`******" &gt;&gt;${LOGFILE}
  
  echo "End of the script"&gt;&gt; ${LOGFILE}
  </code></pre>
  
  <p>And <strong>Output in CSV</strong> i am getting is:</p>
  
  <pre><code>SQL&gt; select * from FIMS_OWNER.TIMELOG_EXTRACT_OUTBOUND_T;
  iv315,29-DEC-14,          8
  DUMMY
  
  REF01
  New Ref Project of type CPRM
  66
  NA
  
  
  iv315,30-DEC-14,          8
  DUMMY
  
  REF01
  New Ref Project of type CPRM
  66
  NA
  
  
  iv315,31-DEC-14,          8
  DUMMY
  
  REF01
  New Ref Project of type CPRM
  66
  NA
  </code></pre>
  
  <p><strong>That is values are then separated by newline(when seen in wordpad)</strong></p>
  
  <p>Thanks</p>