When I run the following code to output an Sql query to a file, it works fine:
exec xp_cmdshell 'bcp "SELECT top 5 * from NK.[dbo].[eclubCustomerDelta]" queryout c:\nk\contacts.txt -c -T'
But I have saved my query in another variable earlier in the code:
SET @result = (SELECT @x,@y
FOR XML PATH('contacts'))
@x
and @y
are themselves variables defined earlier (the query gives the correct output when run reparately). This being said, a similar output of the query does not seem to be working:
exec xp_cmdshell 'bcp "SELECT @result" queryout c:\nk\contacts.txt -c -T'
Here is how my output looks:
Starting copy...
SQLState = 37000, NativeError = 137
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Must declare the scalar variable "@result".
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Unable to resolve column level collations
BCP copy out failed