locked
Export SSMS result set into txt/csv/excel file RRS feed

  • Question

  • Hi All,

    I am using below SQL command to export the file from SSMS resultset.

    EXEC master..xp_cmdshell 'SQLCMD -S servername -d databasename -E -Q "SELECT TOP 2 [Col1]
    FROM [dbo].[TableName]" -b -o \\servername\folder\myoutput.txt',no_output


    When I executed above query from SSMS query window it is throwing the below error.

    Msg 105, Level 15, State 1, Server servername, Line 1
    Unclosed quotation mark after the character string 'SELECT TOP 2 [Col1]
    '.
    Msg 102, Level 15, State 1, Server servername, Line 1
    Incorrect syntax near 'SELECT TOP 2 [Col1]
    '.

    Any help would be greatly appreciated.

    Thanks,


    • Edited by TWoW2017 Thursday, January 18, 2018 4:49 PM
    Thursday, January 18, 2018 4:47 PM

Answers

  • You have a CR/LF between "...[Col1]" and "FROM...".  This needs to be on one line.
    • Marked as answer by TWoW2017 Thursday, January 18, 2018 6:50 PM
    Thursday, January 18, 2018 6:46 PM

All replies

  • Hi, 

    I tested on SSMS v17.4 (14.0.17213.0) and there's no error. What version do you have?

    Thursday, January 18, 2018 5:10 PM
  • What you posted is a general syntax only. Please post your actual command so that we can check for any issues within the command. I mean the exact command which gives you the above error
    • Edited by SQLNeophyte Thursday, January 18, 2018 5:15 PM
    Thursday, January 18, 2018 5:14 PM
  • You have a CR/LF between "...[Col1]" and "FROM...".  This needs to be on one line.
    • Marked as answer by TWoW2017 Thursday, January 18, 2018 6:50 PM
    Thursday, January 18, 2018 6:46 PM
  • Thank you. It is working fine now.
    Thursday, January 18, 2018 6:51 PM