Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header formatting while spooling a csv file in sqlplus

I am required to spool a csv from a table in Oracle, using sqlplus. Following is the format required:

"HOST_SITE_TX_ID","SITE_ID","SITETX_TX_ID","SITETX_HELP_ID"
"664436565","16","2195301","0"
"664700792","52","1099970","0"

Following is the relevant piece of the shell script I wrote:

sqlplus -s $sql_user/$sql_password@$sid << eof >> /dev/null
    set feedback off
    set term off
    set linesize 1500
    set pagesize 11000
  --set colsep ,
  --set colsep '","'
    set trimspool on
    set underline off
    set heading on
  --set headsep $
    set newpage none


    spool "$folder$filename$ext"
    select '"'||PCL_CARRIER_NAME||'","'||SITETX_EQUIP_ID||'","'||SITETX_SITE_STAT||'","'||SITETX_CREATE_DATE||'","'||ADVTX_VEH_WT||'"'
    from cvo_admin.MISSING_HOST_SITE_TX_IDS;
    spool off

(I have used some commented statements in, to signify the things that I tried but couldn't get to work)

The output I receive is:

'"'||PCL_CARRIER_NAME||'","'||SITETX_EQUIP_ID||'","'||SITETX_SITE_STAT||'","'||SITETX_CREATE_DATE||'","'||ADVTX_VEH_WT||'"'
"TRANSPORT INC","113","00000000","25-JAN-13 10.17.51 AM",""
"TRANSPORT INC","1905","00000000","25-JAN-13 05.06.44 PM","0"

Which shows that the header is messed up - it is literally printing the whole string that should have been interpreted as an sql statement, as is the case with the data displayed.

Options I am considering:

1) Using colsep

set colsep '","'
spool
select * from TABLE
spool off

This introduces other problems as the data having leading and trailing spaces, first and the last values in the files are not enclosed by quotes

    HOST_SITE_TX_ID","   SITE_ID"
    "             12345","      16"
    "             12345","      21

I concluded that this method gives me more heartburn than the one I described earlier.

2) Getting the file and use a regex to modify the header.

3) Leaving the header altogether and manually adding a header string at the beginning of the file, using a script

Option 2 is more doable, but I was still interested in asking, if there might be a better way to format the header somehow, so it comes in a regular csv, (comma delimited, double quote bounded) format.

I am looking to do as less hard coding as possible - the table I am exporting has around 40 columns and I am currently running the script for around 4 million records - breaking them in a batch of around 10K each. I would really appreciate any suggestions, even totally different from my approach - I am a programmer in learning.

like image 336
Tech_Coder Avatar asked Jul 31 '13 20:07

Tech_Coder


People also ask

Can CSV files have headers?

CSV and spreadsheet content rules. Each row in the file must contain the same number of cells. This rule also applies to the header row. The first row must contain column headers.

How do I spool a csv file?

Use Spool to Export Query Results to a CSV Filetable WHERE condition; spool off; In order to execute the Spool, you'll need to run it as a script (for example, if you are using Oracle SQL Developer, you may press F5 to run the Spool as a script). Your CSV file will then get created at your specified path.


1 Answers

One easy way to have a csv with just one header is to do

set embedded on
set pagesize 0
set colsep '|'
set echo off
set feedback off
set linesize 1000
set trimspool on
set headsep off

the embedded is a hidden option but it is important to have JUST one header

like image 155
higuita Avatar answered Sep 20 '22 21:09

higuita