If you cannot get your query to work, try the following troubleshooting:
1) You are running a Windows command - bcp - so try running this outside SQL Server. If the command syntax is wrong, it is often easier to get diagnostic information when you run in a Command window than when you run in SQL Server.
2) When you know you have working syntax, try it in SQL Server using an account with sysadmin authority, and look at any SQL Server diagnostics. When you can get the command to work with a sysadmin account you then know you have a process that can work.
3) If you want to run the command using a non-sysadmimn account, log on using that account and try the command. Again look at the SQL Server diagnostics and fix any issues.
The use of xp_cmdshell within SQL Server 2005 or above requires some configuration. The full details are in Books Online (BOL), so I will just give an outline here.
a) Enable xp_cmdshell option within SQL Server.
b) If an account that is not sysadmin is to use xp_cmdshell, you must configure the xp_cmdshell proxy. A single xp_cmdshell proxy is used for allnon-sysadmin use of xp_cmdshell on the SQL instance, and any Windows file permisions that may be needed must be granted to the proxy account.
c) You must grant execute rights to xp_cmdshell for any account that uses it.
The xp_cmdshell proxy account must have Windows execute rights on any program folder containing the commands you execute. Often the proxy account will not have this right on the program folder containging the bcp command, so this right must be granted in Windows. For some sites you may also need to grant execute rigts to cmd.exe in the Windows \system32 folder.
Original author: SQL Server FineBuild 1-click install and best practice configuration of SQL Server 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005. 14 Mar 2017
: now over 40,000 downloads.Disclaimer: All information provided is a personal opinion that may not match reality.Quote: When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist. - Archbishop Hélder Câmara