0

Goal:
I want to add some name of the column's header from stored procedure to the CSV file

bcp "EXEC [databasname].[storedprocedure]" queryout C:\test\bcp_outputTable.csv -c -T -SPC01 -r\n

Problem:
I cannot find a solution to it.

Info:
I'm using the code above in CMD.

HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • Potential duplicate of http://stackoverflow.com/questions/1355876/export-table-to-file-with-column-headers-column-names-using-the-bcp-utility-an – Scott C May 11 '15 at 13:25
  • In this context I cannot change the code of the the stored procedure due to many reason. – HelloWorld1 May 11 '15 at 13:30

2 Answers2

1
rename "C:\test\bcp_outputTable.csv" "C:\test\bcp_outputTable.txt"
echo HeadRow1,HeadRow2,HeadRow3,... >"C:\test\bcp_outputTable.csv"
type "C:\test\bcp_outputTable.txt>>"C:\test\bcp_outputTable.csv"
del "C:\test\bcp_outputTable.txt"
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

@ECHO off

SET "location=c:\test\" SET filename=%date:~0,10%

ECHO This bat creates a csv file to the to the folder %location% ECHO If you are ready, press the button space

PAUSE

BCP "[databasname].[storedprocedure]" queryout %location%%filename%.txt -c -T -t; -SPC01 -r\n

:: Adding header in the csv file ECHO f;f;f> %location%%filename%.csv

TYPE %location%%filename%.txt >> %location%%filename%.csv

DEL /s %location%%filename%.txt

PAUSE

HelloWorld1
  • 13,688
  • 28
  • 82
  • 145