Using the following sqlcmd script:
sqlcmd -S . -d MyDb -E -s, -W -Q \"select account,rptmonth, thename from theTable\"
> c:\\dataExport.csv
<
To remove the Row Count: Add the below to your SQL statement
SET NOCOUNT ON;
To remove the hyphen row try the following upon successful execution:
findstr /v /c:"---" c:\dataExport.csv > c:\finalExport.csv
I use "---" as all my columns are over 3 characters and I never have that string in my data but you could also use "-,-" to reduce the risk further or any delimiter based on your data in place of the ",".
Use the following;
sqlcmd -S . -d MyDb -E -s, -h-1 -W -Q "set nocount on;select 'account','rptmonth', 'thename';select account,rptmonth, thename from theTable"
> c:\dataExport.csv
The guy with the top answer didn't answer how to remove the dashed line. This is my awesome solution.
Sorry I'm 4 years and 9 months late.
In my case worked well as :
type Temp.txt | findstr /v -- > DestFile.txt
I used another solution to solve the issue of removing the dashed line below the header.
DECLARE @combinedString VARCHAR(MAX);
SELECT @combinedString = COALESCE(@combinedString + '|', '') + COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YOUR_TABLE_NAME'
Then just use
Print @combinedString
above your select statement.
I used pipe delimiter.
1.Create the file first with the header columns
2.Apprend the sqlcmd output to the file using the option -h-1
echo acctnum,rptmonth,facilname > c:\dataExport.csv sqlcmd -S . -d MyDb -E -s, -h-1 -W -Q "select account,rptmonth, thename from theTable" >> c:\dataExport.csv