Search code examples
sqlsql-serversqlcmd

Unexpected argument executing cmdexec on a SQL job to export to CSV


I try to run this on a SQL job:

sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" > D:\Test.csv

How can I fix this error?

Sqlcmd: '> D:\Test.csv': Unexpected argument.


Solution

  • Have you tried like this -

    sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" -o D:\Test.csv
    

    where -o output_file which would identify the file that receives output from sqlcmd.

    Additionally you could try BCP which is best suited for bulk coping data between an instance of Microsoft SQL Server and a data file in a user-specified format. Read more here.