Skip to main content
Question Protected by CommunityBot
Tweeted twitter.com/StackDBAs/status/964114506820579329
edited tags
Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663
Source Link
Brandon
  • 618
  • 2
  • 7
  • 17

"Unable to Open BCP host data-file" using xp_cmdshell from SSMS but command line works

I have a stored procedure that runs on a job every once in a while to BCP some files left by an application I've written.

I noticed the files were piling up and BCP wasn't picking them up so I tested in Management Studio using the following:

DECLARE @sql VARCHAR(MAX)
DECLARE @path VARCHAR(512) = 'C:\BCPFiles\'

--Use BCP to copy files in character format from the target directly into the table. 
SET @sql = 'bcp [MyDB].[dbo].[MyTable] in ' + @path + 'bcpFile.dat -c -T'

EXEC master..xp_cmdshell @sql

And got this error message

BCP Error

Executing from the command line on the same server using the same command works no problem.

Here's a few other observations:

  1. I can run EXEC master..xp_cmdshell 'dir C:\*.*' and the results return as expected.
  2. If I copy and rename the file to something different, BCP picks it up
  3. The file isn't being left open by my application: I can move it, rename it, edit and save it, delete it etc... without any complaints from Windows.
  4. The internal format of the file is obviously okay (see 2.)
  5. NT SERVICE\MSSQLSERVER has Full Control permissions to the directory.
  6. The Maple Leafs traded Dion Phaneuf yesterday

Any help is appreciated.