Unable to open BCP host data-file

不想你离开。 提交于 2019-11-30 08:17:34

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.


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.

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\"'

Please check, the file might be opened in another application or program. If it is the case, bcp.exe cannot overwrite the existing file contents.

In my case, I solved The problem in the following way:

my command was :

bcp "select Top 1000 * from abc.dbo.abcd" queryout FileNameWithDirectory -c -t "|" -r "0x0a" -S 192.111.1.111 -U xx -P xxxxx

My FileNameWithDirectory was too long. like "D:\project-abc\R&D\abc-608\FilesNeeded\FilesNeeded\DataFiles\abc.csv".

I change into a simpler directory like : "D:\abc.csv"

Problem solved.

So I guess the problem occurred due to file name exceeding. thus the file was not found.

If it works from the command line but not from the SQL Agent, I think it is an authentication issue.

The SQL Server Agent is running under a account. Make sure that the account has the ability to read the format file and generate the output file.

Also, make sure the account has the ability to execute the xp_cmdshell stored procedure.

Write back with your progress ...

Remove no_output from your command, if you use one offcourse

SET @sql = 'BCP ....'

EXEC master..xp_cmdshell @sql , no_output

EXEC master..xp_cmdshell @sql

In my case this fix was simply running in administrator mode.

In case anyone else runs into the same problem: I had ...lesPerson" queryout' rather than ...lesPerson" queryout '

If your code is writing the data file, and then reading it with BCP, make sure that you CLOSE THE DATA FILE before trying to read it!

Failure to do so gives: 'Unable to open host data-file'.

Python example:

# Management of temporary bulk insert file.

def openBulkInsertFile(self) :
    self.bulkInsertFile = open('c:/tmp/bulkInsertContent.txt', 'w', newline='')
    self.csvWriter = csv.writer(self.bulkInsertFile)

def closeBulkInsertFile(self) :
    self.bulkInsertFile.close()

I received this after I shared my output folder, even when there were no files open.

I created a new, unshared folder for output and all was fine. (might help someone ;-))

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!