I’m trying to create a csv using SQL Command. I am using a simple table as a test. I’ve written this so far:
DECLARE @sql VARCHAR(1000),
@cmd VARCHAR(100),
@sqlCommand VARCHAR(1000)
Set @cmd = 'Select * From DSG.Pawtucket.counts'
SET @sqlCommand = 'SQLCMD -S <server> -U <user> -P <password> -d <database> -W -Q "' + @cmd + '"
-s "|" -o <network path> -h-1'
EXEC master.dbo.xp_cmdshell @sqlCommand
I see the query results in the ‘results’ tab and I receive no errors but my file is never created. I know I am using a valid network path. I even tried just creating the file on the SQL servers local C: drive with no success. I also tried using bcp with much frustration and no results. Any help would be greatly appreciated!
Get rid of the line break in the literal string
What you wrote
Sends two commands to xp_cmdshell
The first command which gives you the results from @cmd in your results tab.
And then this which does nothing
Below is what you want. Aside from removing the carriage return I also added the space after ‘” since
-sneeds to be separated from whatever is in @cmd