I am trying to find an easy way of updating the first row in the export of a sqlcmd query to CT_EVENT,,,,,,,,,,,,,,,,,,,,,,,,. I found this article in stack overflow which builds the csv format. I've also checked the Microsoft documentation for sqlcmd parameters.

sqlcmd -S DatabaseServer1 -Q "EXEC [db1].[dbo].[MyStoredProc];" -o output.tmp -s "," -W
type output.tmp | findstr /r /v "^-[-,]*-.$"  > "c:\Temp\import_template.csv"
del output.tmp

EDIT: I've found the print statement works for this.

PRINT 'CT_EVENT,,,,,,,,,,,,,,,,,,,,,,,,';


  • It would be nice if there was a comment as to why you downvoted. I put a lot of time and effort into this question. – aduguid Jun 27 '18 at 11:46
  • What do you mean by updating the first row? Can you provide an example? – vonPryz Jun 27 '18 at 12:50
  • I would like to replace the entire first row of the file – aduguid Jun 27 '18 at 12:51
  • Are you trying to create a CSV file, but the "Format grouped..." part introduces problems when importing the CSV into somewhere? – vonPryz Jun 27 '18 at 12:52
  • Yes, I basically just want the exported CSV file to be to spec for import into another system. – aduguid Jun 27 '18 at 12:54

How about Powershell? It has mighty rich set of features for string manipulation and it's got built-in CSV support too. A quick and dirty way would be like so,

# Read the file into a variable, skip 1st row
$data = (Get-Content $myFile | Select-Object -Skip 1)
# Write just the header
set-content -path c:\output.csv -value "my,desired,header,column,,,,,"
# Add actual data
add-content -path c:\output.csv -value $data
  • Thanks so much for taking the time to answer. I'll give it a go tomorrow when I'm in the office. – aduguid Jun 27 '18 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.