pavelmed
asked on
sqlplus spool to CSV file with headers truncate column names, other issues
I am trying to run sqlplus from UNIX script to create csv file output with headers
I can generate the csv output, but when column names in headers look truncated or missing or damaged.
I use "select * form table" statement because the table has a lot of columns, and there are a lot of tables to produce output from, so to individually list all columns in the "select from dual" statement would take too much work (hundreds or thousands column names to code).
So I tried to simplify the code by using set colsep "," but it produces many wrong column names.
Do you know how to fix it?
Another question is how to surround string outputs with "" to prevent embedded commas to break the CSV file layout.
This is my code:
-------------------------- ---------- ----------
#!/bin/sh
#echo "Trying to connect to sqlplus..."
sqlplus -s /nolog > /dev/null 2>&1 << EOF
connect user/password@database
set echo off
set feedback off
set linesize 1000
set pagesize 5000
set newpage 0
set colsep ","
set term off
set verify off
set heading
rem set headsep ","
set trim on
set trimspool on
set underline off
set wrap on
spool /usr/lawson/auditrpt.tmp
select * from MYTABLE where OPERATOR='username';
spool off
quit
EOF
exit
__________________________ __________ ___
First few columns of the table should look like this:
VENDOR-GROUP,VENDOR,VEN-CL ASS,VENDOR -VNAME,VEN DOR-SNAME, VENDOR-CON TCT,REMIT- TO-CODE,PU RCH-FR-LOC ,PAY-VENDO R,VENDOR-S TATUS,VEN- PRIORITY, etc.
However, in the file they look like this:
VEND,VENDOR ,VEN,VENDOR_VNAME ,VENDOR_SNAME ,VENDOR_CONTCT ,REMI,PURC,PAY_VENDO,V,VEN _PRIORITY, etc.
As you can see, some column names are displayed properly, and some are not.
The data output seems correct though.
Please assist.
Thank you in advance.
I can generate the csv output, but when column names in headers look truncated or missing or damaged.
I use "select * form table" statement because the table has a lot of columns, and there are a lot of tables to produce output from, so to individually list all columns in the "select from dual" statement would take too much work (hundreds or thousands column names to code).
So I tried to simplify the code by using set colsep "," but it produces many wrong column names.
Do you know how to fix it?
Another question is how to surround string outputs with "" to prevent embedded commas to break the CSV file layout.
This is my code:
--------------------------
#!/bin/sh
#echo "Trying to connect to sqlplus..."
sqlplus -s /nolog > /dev/null 2>&1 << EOF
connect user/password@database
set echo off
set feedback off
set linesize 1000
set pagesize 5000
set newpage 0
set colsep ","
set term off
set verify off
set heading
rem set headsep ","
set trim on
set trimspool on
set underline off
set wrap on
spool /usr/lawson/auditrpt.tmp
select * from MYTABLE where OPERATOR='username';
spool off
quit
EOF
exit
__________________________
First few columns of the table should look like this:
VENDOR-GROUP,VENDOR,VEN-CL
However, in the file they look like this:
VEND,VENDOR ,VEN,VENDOR_VNAME ,VENDOR_SNAME ,VENDOR_CONTCT ,REMI,PURC,PAY_VENDO,V,VEN
As you can see, some column names are displayed properly, and some are not.
The data output seems correct though.
Please assist.
Thank you in advance.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>>because I would need to code many tens column names per table
I'm not following what you are saying. If you are referring to my example, I didn't hard-code any column names.
I also don't see any hard-coding in the links sjwales posted but I might have missed them.
I'm not following what you are saying. If you are referring to my example, I didn't hard-code any column names.
I also don't see any hard-coding in the links sjwales posted but I might have missed them.
ASKER
you did not hardcode any column names but in the select statement you listed columns by names, like this:
select col1 || ',' || col2 || ',' || col3 from tab1;
to have them separated by comma. That was what I was referring to.
Thank you.
select col1 || ',' || col2 || ',' || col3 from tab1;
to have them separated by comma. That was what I was referring to.
Thank you.
OH. I understand.
Then check the links posted by sjwales. The one from AskTom should work.
Then check the links posted by sjwales. The one from AskTom should work.
ASKER
I ended up listing columns by using select 'col1', 'col2' from dual
and hardcoding surrounding double quotes in a select query return.
It was a log of coding but I could retrieve the tables' columns layout separately and I just needed to modify it by inserting commas and quotes.
Thank you for your help.
and hardcoding surrounding double quotes in a select query return.
It was a log of coding but I could retrieve the tables' columns layout separately and I just needed to modify it by inserting commas and quotes.
Thank you for your help.
ASKER
That's why I was trying to use "select * from table" statement.
Is there any other solution?
Thank you.