Unable to open BCP host data-file

First, rule out an xp_cmdshell issue by doing a simple 'dir c:*.*';

Check out my blog on using BCP to export files.

I had problems on my system in which I could not find the path to BCP.EXE.

Either change the PATH variable of hard code it.

Example below works with Adventure Works.

-- BCP - Export query, pipe delimited format, trusted security, character format
DECLARE @bcp_cmd4 VARCHAR(1000);
DECLARE @exe_path4 VARCHAR(200) = 
    ' cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ & ';
SET @bcp_cmd4 =  @exe_path4 + 
    ' BCP.EXE "SELECT FirstName, LastName FROM AdventureWorks2008R2.Sales.vSalesPerson" queryout ' +
    ' "C:\TEST\PEOPLE.TXT" -T -c -q -t0x7c -r\n';
PRINT @bcp_cmd4;
EXEC master..xp_cmdshell @bcp_cmd4;
GO

Before changing the path to \110\ for SQL Server 2012 and the name of the database to [AdventureWorks2012], I received the following error.


enter image description here

After making the changes, the code works fine from SSMS. The service is running under NT AUTHORITY\Local Service. The SQL Server Agent is disabled. The output file was created.

enter image description here


Does the output path exist? BCP does not create the folder before trying to create the file.

Try this before your BCP call:

EXEC xp_cmdshell 'MKDIR "E:\Storage\Export\Data\"'