Having an issue with exporting files to a network drive via the BCP host data file.

There is no problem in exporting a file to the C drive of the machine that the SQL Server is installed on, however when exporting to a network drive (Same domain) I receive the following error:

*SQLState = S1000, NativeError = 0

Error = [Microsoft][ODBC Driver 11 for SQL Server]

Unable to open BCP host data-file NULL*

The test code is

    DECLARE @sql VARCHAR(8000);
    DECLARE @Path varchar(max)
    SET @Path = '\\network\location\' 
    SELECT @sql = 'bcp "SELECT  FROM [Servername].[Schema].[Table]" queryout ' 
+ @Path + '-c -t, -T -S' 

    exec master..xp_cmdshell @sql;

I have taken a look at the other examples around this issue, however still no joy. What other area should be considered in rectifying this issue..

  • 1
    Does the service that's running SQL have permissions to the UNC path in question? – Jonathan Fite Jul 22 '19 at 12:23
  • 1
    Your @Path variable doesn't specify a filename to write the data to. Also, if you have spaces in your path name, you need to include double quotes (") around the path. Also, verify that SQL Server has permissions to the share and underlying file system to write data. – HandyD Jul 23 '19 at 2:30
  • If you intend to write dynamic tsql, you need to be able to see the code you generate. Before you execute your string, select or print it so you can debug it. – SMor Jul 23 '19 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.