37

For some weird reason I'm having problems executing a bulk insert.

BULK INSERT customer_stg
FROM 'C:\Users\Michael\workspace\pydb\data\andrew.out.txt'
WITH
(
    FIRSTROW=0,
    FIELDTERMINATOR='\t',
    ROWTERMINATOR='\n'
)

I'm confident after reading this that I've setup my user role correctly, as it states...

Members of the bulkadmin fixed server role can run the BULK INSERT statement.

I have set the Login Properties for the Windows Authentication correctly (as seen below).. to grant server-wide permissions on bulkadmin

windows authentication
(source: iforce.co.nz)

And the command EXEC sp_helpsrvrolemember 'bulkadmin' tells me that the information above was successful, and the current user Michael-PC\Michael has bulkadmin permissions.

bulkadmin
(source: iforce.co.nz)

But even though I've set everything up correctly as far as I know, I'm still getting the error. executing the bulk insert directly from SQL Server Management Studio.

Msg 4861, Level 16, State 1, Line 2
Cannot bulk load because the file "C:\Users\Michael\workspace\pydb\data\andrew.out.txt" could not be opened. Operating system error code 5(Access is denied.).

which doesn't make sense because apparently bulkadmins can run the statement, am I meant to reconfigure how the bulkadmin works? (I'm so lost). Any ideas on how to fix it?

7
  • Sounds like the problem is external to SQL Server. Can you open the file in Notepad?
    – lc.
    Jan 28, 2013 at 4:08
  • Yeah I can open it in Notepad. Jan 28, 2013 at 4:15
  • 1
    If the server is not local, social.msdn.microsoft.com/Forums/en/transactsql/thread/… could be relevant
    – lc.
    Jan 28, 2013 at 4:17
  • for now I'm just going to try and do a fresh install of SQL Server, as it just doesn't make logical sense in why I'm being restricted from accessing a file. Jan 28, 2013 at 4:40
  • 1
    Can you set the path to the file in a way that it is accessible from the network, say "Michael-PC\C$\...\Andrew.out.txt" and pass this to the command line? Jan 28, 2013 at 5:29

12 Answers 12

29

This error appears when you are using SQL Server Authentication and SQL Server is not allowed to access the bulk load folder.

So giving SQL server access to the folder will solve the issue. enter image description here

Here is how to: Go to the folder right click ->properties->Security tab->Edit->Add(on the new window) ->Advanced -> Find Now. Under the users list in the search results, find something like SQLServerMSSQLUser$UserName$SQLExpress and click ok, to all the dialogs opened.

6
  • I couldn't find the correct account to grant access to, but as I was just trying to load a file from my development machine, I granted read permission on the file to "Everyone" (that's what I entered as the account name / principal).
    – Giles
    Mar 24, 2015 at 15:01
  • 4
    necro, but there's official documentation, which may be easier: learn.microsoft.com/en-us/sql/database-engine/configure-windows/… - basically, add the NT SERVICE\MSSQLSERVER user
    – Jeutnarg
    Apr 2, 2018 at 18:18
  • 1
    I couldn't figure out which user to add the permissions to either, so just used Everyone. I can't find a user called "NT SERVICE\MSSQLSERVER"
    – Paul
    Jul 6, 2020 at 12:26
  • where the heck is the "bulk load folder"? Is that meaning the folder in which the file you are trying to load is stored? Dec 10, 2020 at 23:21
  • 2
    @AlbatrossCafe yes bulk load folder just means the folder where the file is you're BULK INSERTing. For me adding permissions to the folder for the user called SERVICE worked.
    – Paul
    Jan 19, 2021 at 17:21
13

I don't think reinstalling SQL Server is going to fix this, it's just going to kill some time.

  1. Confirm that your user account has read privileges to the folder in question.
  2. Use a tool like Process Monitor to see what user is actually trying to access the file.
  3. My guess is that it is not Michael-PC\Michael that is trying to access the file, but rather the SQL Server service account. If this is the case, then you have at least three options (but probably others):

    a. Set the SQL Server service to run as you.
    b. Grant the SQL Server service account explicit access to that folder.
    c. Put the files somewhere more logical where SQL Server has access, or can be made to have access (e.g. C:\bulk\).

I suggest these things assuming that this is a contained, local workstation. There are definitely more serious security concerns around local filesystem access from SQL Server when we're talking about a production machine, of course this can still be largely mitigated by using c. above - and only giving the service account access to the folders you want it to be able to touch.

1
  • Its just a machine that I'm using to build and test the software, not the production system. Jan 28, 2013 at 7:55
5

I had the same problem SSIS 2012 and the solution was to use Windows Authentication. I was using SQL authentication with the sa user.

1
5
  1. Go to start run=>services.msc=>SQL SERVER(MSSQLSERVER) stop the service
  2. Right click on SQL SERVER(MSSQLSERVER)=> properties=>LogOn Tab=>Local System Account=>OK
  3. Restart the SQL server Management Studio.
1
  • Out of all of the numerous suggested solutions I have found on the web, this one actually solved my problem. Thank you, @Vinay Patel
    – Lucas T
    Nov 2, 2018 at 9:32
4

Try giving the folder(s) containing the CSV and Format File read permissions for ‘MSSQLSERVER’ user (or whatever user the SQL Server service is set to Log On As in Windows Services)

1
  • thanks a lot - following @antew 's answer I tried for find SQL or MSSQL in the list but can't. I see MSSQL is log on as "local service" in windows service. I added "local service" to security and fixed the permission issue.
    – YJZ
    Sep 15, 2016 at 3:04
3

This is what worked for me:

Log on SSIS with Windows authentication.

1. Open services and find MSSQL NT Service account name and copy it:

enter image description here

2. Open folder from which SQL server should read from. Security - Group or user names tab - Add and paste there copied account:**

enter image description here

  1. You will probably get "Multiple names found error", just select MSSQL user:

enter image description here

Your BULK INSERT query should run fine now. If problem persists try adding SQL Server Agent account to folder permissions in same way. Make sure you restart MSSQL server in services after you are done.

2

This is quite simple the way I resolved this problem:

  1. open SQL Server
  2. right click on database (you want to be backup)
  3. select properties
  4. select permissions
  5. select your database role (local or cloud)
  6. in the you bottom you will see explicit permissions table
  7. find " backup database " permission and click Grant permission .

your problem is resolved .

2

sometimes this can be a bogus error message, tried opening the file with the same account that it is running the process. I had the same issue in my environment and when I did open the file (with the same credentials running the process), it said that it must be associated with a known program, after I did that I was able to open it and run the process without any errors.

2

Make sure the file you're using ('C:\Users\Michael\workspace\pydb\data\andrew.out.txt') is on the SQL server machine and not the client machine running MSSMS.

2
  • The client machine is the sql server (development machine) Jul 10, 2017 at 10:14
  • VERY good point
    – JGlass
    Feb 14, 2022 at 19:48
1

1) Open SQL 2) In Task Manager, you can check which account is running the SQL - it is probably not Michael-PC\Michael as Jan wrote.

The account that runs SQL need access to the shared folder.

1

I have come to similar question when I execute the bulk insert in SSMS it's working but it failed and returned with "Operation system failure code 5" when converting the task into SQL Server Agent.

After browsing lots of solutions posted previously, this way solved my problem by granting the NT SERVER/SQLSERVERAGENT with the 'full control" access right to the source folder. Hope it would bring some light to these people who are still struggling with the error message.

1

In our case it ended up being a Kerberos issue. I followed the steps in this article to resolve the issue: https://techcommunity.microsoft.com/t5/SQL-Server-Support/Bulk-Insert-and-Kerberos/ba-p/317304.

It came down to configuring delegation on the machine account of the SQL Server where the BULK INSERT statement is running. The machine account needs to be able to delegate via the "cifs" service to the file server where the files are located. If you are using constrained delegation make sure to specify "Use any authenication protocol".

If DFS is involved you can execute the following Powershell command to get the name of the file server:

Get-DfsnFolderTarget -Path "\\dfsnamespace\share"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.