Sqlcmd to generate file without dashed line under header, without row count

后端 未结 8 1759
难免孤独
难免孤独 2021-02-01 06:09

Using the following sqlcmd script:

sqlcmd -S . -d MyDb -E -s, -W -Q \"select account,rptmonth, thename from theTable\"  
> c:\\dataExport.csv
<
相关标签:
8条回答
  • 2021-02-01 06:50

    In addition, if you want to query out all records in a table, you can code as

    1. SET NOCOUNT ON;
    2. SELECT SUBSTRING((SELECT ','+ COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=N'%table_name%' FOR XML PATH('') ), 2, 9999);
    3. SELECT * FROM %table_name%

    Assign the above queries into a variable %query%. The the command will be looks like as below.

    SQLCMD -h -1 -W -E -S %sql_server% -d %sql_dabase% -Q %query% -s"," -o output_file.csv
    
    0 讨论(0)
  • 2021-02-01 06:55

    Solutions:

    1) To remove the row count ("(139 rows affected)") you should use SET NOCOUNT ON statement. See ref.

    2) To remove column headers you should use -h parameter with value -1. See ref (section Formatting Options).

    Examples:

    C:\Users\sqlservr.exe>sqlcmd -S(local)\SQL2012 -d Test -E -h -1 -s, -W -Q "set nocount on; select * from dbo.Account" > d:\export.txt. 
    

    or

    C:\Users\sqlservr.exe>sqlcmd -S(local)\SQL2012 -d Test -E -h -1 -s, -W -Q "set nocount on; select * from dbo.Account" -o "d:\export2.txt"
    
    0 讨论(0)
提交回复
热议问题