-- Here are two different ways to export comma-separated column names for the header line in spreadsheets
-- Substitute the schema and table names for the table data being exported, and concatenate the table data to the column names.
-- Simple!

-- Alex Levy t/a http;//www.sustainablesoftware.net, March 2008

-- the recursive way:
with list (colname,colno) as 
             (select colname,colno 
              from   syscat.columns 
              where  tabschema = 'MYSCHEMA'  
              and    tabname = 'MYTABLE'), 
     serial_list(s_colname,s_colno) as 
                (select rtrim(colname)||',', 
                        colno  
                 from   list  
                 where  colno = 0 
                        UNION ALL 
                 select long_varchar(s_colname||rtrim(colname)||','), 
                        colno 
                 from   list t, serial_list s 
                 where  s.s_colno +1 = t.colno ) 
select trim(trailing ',' from rtrim(s_colname))  
from   serial_list 
where  s_colno = (select max(s_colno) from serial_list) 
;

-- using powerful XML functions: 
-- with grateful thanks to Rob Jackson-Jones of IBM.

export to  /tmp/header.csv of del modified by chardel@
select substr(replace(replace(xmlserialize(content xmlagg(xmlelement(name C,colname) order by colno) as varchar(1500)),'<C>',', '),'</C>',''),3) 
from syscat.columns 
where tabschema='MYSCHEMA' 
and tabname='MYTABLE'
;

-- and using the LISTAGG function:
select listagg(cast(colname as varchar(30000)),',') within group(order by colno) as answer
from   syscat.columns
where  tabschema = 'MYSCHEMA'
and        tabname = 'MYTABLE'
;