Re: CSV file with Headers Thanks very much for your help, I'll try this!
Bye!
esmith2112 ha escrito:
> I've been challenged by this in the past since I'm require to deliver
> data sets to other systems on a recurring basis. Since DB2 can output a
> nicely formatted and largely compatible CSV format through its EXPORT
> utility, I cobbled together a UNIX Korn shell script to do the dirty
> work for me. It's not elegant, but works for me.
>
> It's currently implemented for AIX and relies on the output from the
> DESCRIBE utility (v 7.2) to get the column headers as they would be
> returned in a cursor. (It may have to be tweaked if the format has
> changed for later versions of DB2 or other types of OS.) It assumes you
> have an SQL statement terminated with a semi-colon in a file which
> would be passed to the script as the first argument. I named the script
> "csv" and would execute it like this:
>
> $> csv calctotal.sql
>
> which create an output file called:
>
> calctotal.sql.csv
>
>
>
> Here's the script:
> ------------------------------------------------------------------------------------------------
> #!/bin/ksh
>
> db=yourdatabase
> dbuser=youruserid
> dbpasswd=yourpassword
>
> FILE=$1
> tmpfile=/tmp/csv.$$
> outfile=$FILE.csv
>
> ## connect to database
> db2 connect to $db user $dbuser using $dbpasswd
>
> ## create statement to materialize headers
> print "describe" > $tmpfile
> cat $FILE >> $tmpfile
>
> ## get column headers from db2 output and
> ## assemble into comma separated list
> db2 -tf $tmpfile|grep -E "^ 3|^ 4"|cut -c32-62|while read colname
> do
> colhdr="$colhdr,$colname"
> done
>
> ## strip of leading comma
> colhdr=$(echo $colhdr|cut -f2- -d,)
>
> ## create file used to export
> print "export to /tmp/data.$$ of del" > $tmpfile
> cat $FILE >> $tmpfile
>
> ## execute export SQL
> db2 -tf $tmpfile
>
> ## assemble headers + data
> print $colhdr > $outfile
> cat /tmp/data.$$ >> $outfile
>
> ## clean up
> rm /tmp/*.$$ |