I can't seem to find a way to retain the column headers using xp_cmdshell bcp
to export a .csv
or .txt
file. Currently I'm using this code to export the tables to a txt file:
SET @sqlsave = 'master.sys.xp_cmdshell ''bcp "SELECT * FROM DDMVMS.dbo.'+@SelectedTable+'" queryout "'+@locationoutputtxt+'" -c -T -t; -S localhost\SQLEXPRESS'''
EXEC (@sqlsave);
This line works well, but the headers of the columns are not retained in the txt file. I can't find an easy way to retain them. I use this code in a loop, so adding them manually takes too much time.
Thank you!