Search code examples
sqlsql-servercsvbcp

BCP.exe Out not identifying table so it can be written to a file


When I run:

EXEC MASTER..xp_cmdshell 'bcp [ServerName\Instance].[DB_Name].[dbo].[r] out
 "C:\0\folder\subfolder\filename.csv" -c -t "|" -T' 

I get the error message:

A valid table name is required for in, out, or format options.

My goal is to simply export the contents of the table named r to a pipe delimited file. The line is used multiple times in the script with a different output file name. The table r is dropped and recreated multiple times with different content each time from a different table (not in a loop or cursor). The error occurs at the first time the code block above is run.

  • This is on SQL Server 2014, Management Studio 12.0.5000.
  • This script ran just fine when the volume storing the DB was mounted on another machine running SQL Server 2014 as well.
  • I have run: EXEC sp_configure 'show advanced options', 1 EXEC sp_configure 'xp_cmdshell', 1 along with: RECONFIGURE
  • I am the DB owner.
  • The local path "C:\0\folder\subfolder\" exists.
  • I can successfully select and view the table contents with the query:

    `Select * From [ServerName\Instance].[DB_Name].[dbo].[r]`
    
  • Prior to adding the code [ServerName\Instance], the error msg said that it couldn't open a connection.

  • There are three results that show up when I search for the error message above and none of them address my issue.

Solution

  • As @James Z noted you need to specify server_name[\instance_name]. Also you need to remove [ServerName\Instance]. before [DB_Name]...

    Thus, you command should look like this:

    EXEC MASTER..xp_cmdshell 'bcp [DB_Name].[dbo].[r] out
     "C:\0\folder\subfolder\filename.csv" -c -t "|" -T -S ServerName\Instance'