0

The following lines are working fine and exporting as CSV. I want all the comma-separated values in double-quotes. I want to export the results of SELECT * FROM [AbcSQL].[dbo].[Customer] WHERE... into CSV with all output values in "123" this format

BEGIN
Declare @custid as int
Declare @qry as varchar(8000)
Declare @cmd varchar(8000)

set @trcustomerid = 1234

set @qry = 'set nocount on; SELECT * FROM [AbcSQL].[dbo].[Customer] WHERE trcustomerid= ' + cast(@custid as nvarchar(10)) + '' 


SET @cmd = 'sqlcmd -s, -S serverdetails, -W -Q "' + @qry + '" | findstr /v /c:"-" /b > "c:\Data\trCustomer.csv"'
EXEC master..xp_cmdshell @cmd
END
Dale K
  • 25,246
  • 15
  • 42
  • 71
Aayush
  • 11
  • 1
  • 5
  • Does this answer your question? [SQLCMD : Can we put double quotation mark to output file ?](https://stackoverflow.com/q/52511532/1220550) – Peter B May 03 '21 at 09:46
  • The first duplicate https://stackoverflow.com/a/52528375/14868997 uses Powershell, which is what you should be using. `xp_cmdshell` has security issues, you really shouldn't be using it in production. – Charlieface May 03 '21 at 12:19

0 Answers0