Hey,
I know this is a very old thread but I figured I might as well add what I found.
This is an example query that gave my the syntax error:
select @sql = 'bcp "SELECT * FROM syslogins"
queryout J:\bcp.csv -c -t, -T -S' + @@servername
exec master..xp_cmdshell @sql
My issue was my line break in my @sql string.
Basically, instead of being interpreted as
bcp "SELECT * FROM syslogins" queryout J:\bcp.csv -c -t, -T -S<server>
the cmd exec would run this
bcp "SELECT * FROM syslogins"
queryout J:\bcp.csv -c -t, -T -SUSGDC12S004-DEV\DBA
This means it would only run this part "bcp "SELECT * FROM syslogins"" as it's the first line.
All I had to do was put everything back on the same line like so:
select @sql = 'bcp "SELECT * FROM syslogins" queryout J:\bcp.csv -c -t, -T -S' + @@servername
exec master..xp_cmdshell @sql
I hope this makes sense.