Link to home
Create AccountLog in
Avatar of pavelmed
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-CLASS,VENDOR-VNAME,VENDOR-SNAME,VENDOR-CONTCT,REMIT-TO-CODE,PURCH-FR-LOC,PAY-VENDOR,VENDOR-STATUS,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.
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of pavelmed
pavelmed

ASKER

Thank you, but I was trying to avoid list columns in the sql statement because I would need to code many tens column names per table, and then to repeat it for tens on other tables, so it will be many hundreds (or more than a thousand) column names.

That's why I was trying to use "select * from table" statement.
Is there any other solution?

Thank you.
>>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.
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.
OH.  I understand.

Then check the links posted by sjwales. The one from AskTom should work.
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.