How to allow remote MSSQL user to bulk insert my local txt file?

All we need is an easy explanation of the problem, so here it is.

I have a txt file on my local PC. I run localy Java app which connects to remote MSSQL. Now I need to bulk insert from that text file into my database table.

However, SQL login can’t read my file. I get:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot
bulk load because the file "D:/My_Files/my_file.txt" could not be opened.
Operating system error code 21(The device is not ready.)

I set permissions on My_Files folder for ‘Everyone’. I can’t create a Windows user with the same name like SQL login because it is too long for Windows user. Is there a solution?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

As mustaccio pointed out, a local path is not going to be accessible from the SQL Server, which is the error you’re receiving regarding "D:/My_Files/my_file.txt. It’s looking for a file on the D: drive on the server that your SQL instance is installed on (not your local computer’s D: drive).

You may be able to create a network share to that folder and reference it by UNC path in the SQL Server instance though. How to do that is more so in the scope of a ServerFault question.

Alternatively you can add code in your Java program to read the local file and then do a BULK INSERT command from the app to your database.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply