Friday, January 27, 2006

Adding Column Names to an Unload File

I received an e-mail from a reader asking an interesting question. She wanted to know if any of the DB2 unload utilities are able to include the column names in the same file as the unload output data. This was a requirement because one of the applications her company was planning to use takes the column headings and dynamically creates tables.

My initial reaction was that she could write a program that take the output file and reads the system catalog to gather the appropriate column names. That would work, but she actually came up with a better solution.

She used a third party Unload utility (but it would also have worked with IBM's Unload, too) to perform two SELECT statements. The first just creates the column headers and the second for the actual data. The column and data goes to two separate datasets, but they used IDCAMS to concatenate the 2 separate column/data sets into 1 dataset.

The SQL is simple, something like as follows:

SELECT 'COLUMN NAME 1', 'COLUMN NAME 2',
'COLUMN NAME 3', 'COLUMN NAME 4'
FROM SYSIBM.SYSDUMMY1

SELECT COL1, COL2, COL3, COL4
FROM XXXXXX.TABLE1

Of course, you'd just plug in the correct column names in the literals of the first SELECT statement -- and the correct column names in the second.

No comments: