Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot bulk load. Operating system error code 5 (Access is denied.)

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?

like image 817
classicjonesynz Avatar asked Jan 28 '13 04:01

classicjonesynz


People also ask

Can not bulk load?

The reason that you receive this error message is that you are using SQL Server Authentication and the SQL Server hasn't accessed the bulk load folder. All you need to do is to give SQL Server access to the folder.


2 Answers

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.

like image 63
antew Avatar answered Sep 19 '22 18:09

antew


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.

like image 27
Aaron Bertrand Avatar answered Sep 17 '22 18:09

Aaron Bertrand