7

Having problem using BCP via xp_cmdshell.

I exported the table data to a file as .dat but when I try to import the file to a new table, I am getting the below error. The SQL Server service and the account that I am using has full access on that drive. I am not sure why I am still having issues.

Error = [Microsoft][SQL Server Native Client 10.0] Unable to open BCP host data-file.

Here are the scripts that I am running.

exec master..xp_cmdshell 'BCP database.dbo.tabel OUT d:\tabledata.dat -T -c' 
exec master..xp_cmdshell 'BCP database.dbo.tabelnew IN d:\tabledata.dat -T -c' 
Mark Storey-Smith
31.9k9 gold badges91 silver badges125 bronze badges
asked Dec 30, 2012 at 21:57
3
  • Have you tried executing these commands directly from the command line instead of using xp_cmdshell? Commented Dec 10, 2013 at 21:42
  • 1
    In my experience, BCP generally requires that the file be local to the destination server. Is that the case? Commented Dec 10, 2013 at 23:13
  • Try `exec master..xp_cmdshell 'BCP database.dbo.tabel OUT d:\somefolder\tabledata.dat -T -c'. That should work. Note somefolder. Commented Dec 11, 2013 at 4:26

1 Answer 1

5

I had the same - somehow a windows or SQL Server update changed something and a previously successful build started failing. In my case it was access permissions to the folder containing the dat files that was failing. I could run the bcp from the command line and it worked but it failed from SQL Server. The SQL Service needs access to the folder, basically. To test this, try running

EXEC master..xp_cmdshell 'DIR <your-dat-folder>'

and if that fails check your access permissions (and grant as needed).

answered Sep 30, 2014 at 9:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.