Monday, July 29, 2013

BCP on SQL server and how to export metadata like table headers

A known limitation using "bcp" utility in SQL Server (2k5,2k8,2012) is that there is no way to include meta-data (say header names) to any exported table. There are some trick you can do to overcome such problem.
The basic idea is to create two ascii files, one with the header and one with the data. Then, you can create the final file that is the result of the concatenation of the above. Note the trick that is used in order to get a string that contains the columns of the respected table with the use of the global temp table.
I hope that this solution (that is indeed not very elegant) will help any friend facing the same problem.