I'm trying to insert a .JPG into a SQL Server database using this query:
INSERT INTO Model (Id)
SELECT 1
FROM Openrowset( Bulk 'C:\Desktop\boat.jpg', Single_Blob) as ProductPicture
I'm getting this error:
Operating system error code 3(The system cannot find the path specified.).
I have checked the file path and it seems to be correct.
2 possibe issues:
Permission. YOU are doing nothing - SQL Server is, so the SQL Server user needs permissions to access the file.
Locality. Just because YOU find the file at C:\Desktop does not mean that your SQL Server does. The SQL is executed ON THE SERVER, not on your client.
Given that the path includes DESKTOP it is likely both.
OPENROWSET with a drive letter is referring to the drive on the SQL Server machine, not your PC.
For details of how to use a network share (for example to access your PC), see this MSDN
https://msdn.microsoft.com/en-au/library/ms175915.aspx#Anchor_3
And this for OPENROWSET in general
https://msdn.microsoft.com/en-us/library/ms190312.aspx
Related
Bulk insert is not working when I try to read in the .CSV file data from the network path, it throws an error message saying
Cannot bulk load because the file couldn't be opened Operating system error code 5 (Access is denied)
If I try to bulk insert the same file data from the local drive of the server where SQL Server is running, it works fine without any issue. I checked the access rights with the network path, I do have full access. I need help to resolve this issue.
The data file name must specify a valid path from the server on which SQL Server is running.
If the data file is a remote file, specify the Universal Naming Convention (UNC) name.
A UNC name has the form :
\\Systemname\ShareName\Path\FileName.
For example,
\\SystemX\DiskZ\Sales\update.txt.
For more details: https://learn.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql?view=sql-server-2017
I am running the following query in SQL Server 11.0.
BULK INSERT [dbo].[Andy] FROM "Y:\users\joe\APP data main file.csv"
WITH (FIRSTROW = 2, FIELDTERMINATOR = ',' ,
ROWTERMINATOR = '\n', TABLOCK)
The command is returning the error,
Cannot bulk load because the file "Y:\users\joe\APP data main
file.csv" could not be opened. Operating system error code 3(The
system cannot find the path specified.).
The cause of this error, from researching it online, is that the Bulk Insert task runs by executing the Bulk Insert command from the target SQL server to load the file. This means that the SQL Server Agent of the target SQL server should have permissions on the file I am trying to load. How can I find out whether it does or not?
Secondly, according to Microsoft’s website, “…if a SQL Server user logs on by using Windows Authentication, the user can read only those files that can be accessed by the user account, regardless of the security profile of the SQL Server process.”
I log onto SQL Server using Windows Authentication. By the definition on the MS website, I have access to the file I am trying to insert from. I created it, and I can open it. More specifically, the Windows account I’ve logged onto (my own) is the account that created the csv file, and is the account through which I open SQL Server. Yet the error is there. There is something I am missing, but I have no idea what it is.
Finally, it doesn’t matter whether or not I use the UNC path to access the file, or the drive mapping.
Looks like the file is located on network server, you are referencing mapped drive name, try use UNC network path instead.
I had an email back from my IT department on this question:
The problem I think is that you’re assigned to a server role based on
the user policy group that you’ve been assigned to - which would
correspond with a particular server role - by the network
administrators and this server role doesn’t have access to the file
path for whatever reason. You may have access to the server path
yourself because of another group to which you currently are assigned
but if that group doesn’t correspond with the server role, the server
role won’t have access.
So there we have it. It has since transpired that I can't bulk upload anyway for operational reasons, so I won't be pursuing this any further.
Thanks all for your help.
Accidental DBA here:
I have a variety of CSV exports from my UNIX ERP system that I'm trying to fetch from SQL Server 2014 instead of transferring to my local drive and uploading from there. I have to execute the exports from the ERP system which then sends them to FTP. When I use the Bulk Insert commands I get this error:
Operating system error code 3(The system cannot find the path specified.)
Code for reference:
BULK
INSERT [SALES].DBO.[CUSTOMER-MASTER]
FROM '/tmp/export/customer.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
I don't know what the correct file path is nor where to find it. How do I figure this out?
The path must be either file in the SQL Server's disc, or a windows share (SMB) that the account that the server is running can access using an UNC Path. From the documentation:
data_file must specify a valid path from the server on which SQL
Server is running. If data_file is a remote file, specify the
Universal Naming Convention (UNC) name. A UNC name has the form
\\Systemname\ShareName\Path\FileName. For example,
\\SystemX\DiskZ\Sales\update.txt.
I'm trying to set up a Stored Procedure as a SQL Server Agent Job and it's giving me the following error,
Cannot bulk load because the file "P:\file.csv" could not be opened. Operating system error code 3(failed to retrieve text for this error. Reason: 15105). [SQLSTATE 42000] (Error 4861)
Funny thing is the Stored Procedure works just fine when I execute it manually.
The drive P: is a shared drive on Windows SQL Server from LINUX via Samba Share and it was set up by executing the following command,
EXEC xp_cmdshell 'net use P: "\lnxusanfsd01\Data" Password /user:username /Persistent:Yes'
Any help on this would be highly appreciated
I do not know if you solved this issue, but I ran into the same issue. If the instance is local you must check the permission to access the file, but if you are accessing from your computer to a server (remote access) you have to specify the path in the server, so that means to include the file in a server directory, that solved my case.
example:
BULK INSERT Table
FROM 'C:\bulk\usuarios_prueba.csv' -- This is server path not local
WITH
(
FIELDTERMINATOR =',',
ROWTERMINATOR ='\n'
);
To keep this simple, I just changed the directory from which I was importing the data to a local folder on the server.
I had the file located on a shared folder, I just copied my files to "c:\TEMP\Reports" on my server (updated the query to BULK INSERT from the new folder). The Agent task completed successfully :)
Finally after a long time I'm able to BULK Insert automatically via agent job.
Best regards.
I have solved this issue,
login to server computer where SQL Server is installed get you csv
file on server computer and execute your query it will insert the
records.
If you will give datatype compatibility issue change the datatype for that column
Using SQL connection via Windows Authentication:
A "Kerberos double hop" is happening: one hop is your client application connecting to the SQL Server, a second hop is the SQL Server connecting to the remote "\\NETWORK_MACHINE\". Such a double hop falls under the restrictions of Constrained Delegation and you end up accessing the share as Anonymous Login and hence the Access Denied.
To resolve the issue you need to enable constrained delegation for the SQL Server service account. See here for a good post that explains it quite well
SQL Server using SQL Authentication
You need to create a credential for your SQL login and use that to access that particular network resource. See here
I would suggest the P: drive is not mapped for the account that sql server has started as.
It's probably a permissions issue but you need to make sure to try these steps to troubleshoot:
Put the file on a local drive and see if the job works (you don't necessarily need RDP access if you can map a drive letter on your local workstation to a directory on the database server)
Put the file on a remote directory that doesn't require username and password (allows Everyone to read) and use the UNC path (\server\directory\file.csv)
Configure the SQL job to run as your own username
Configure the SQL job to run as sa and add the net use and net use /delete commands before and after
Remember to undo any changes (especially running as sa). If nothing else works, you can try to change the bulk load into a scheduled task, running on the database server or another server that has bcp installed.
I did try giving access to the folders but that did not help.
My solution was to make the below highlighted options in red selected for the logged in user
My question is about where I should put a file I need to use in a BULK INSERT command in MS SQL.
I have a database running on a server. I run queries on this through an ODBC connection from my machine on the same network. I want to create a stored procedure that will use Bulk Insert to import data from a .txt file and then execute this stored procedure from my machine (from clicking a button in an Excel sheet).
I'm no expert on how SQL Server actually works to say the least so I have what I imagine is a very basic question for someone who does. Does the .txt file used in the Bulk Insert need to be in a location that can be read by:
a) my machine e.g. on it's local hard disk
or
b) on a location that can be read by the database server e.g. somewhere on the network that it can access
I'm not sure if my local machine or the server is actually opening the file. I would assume it's the server, but I'd like to be sure!
Many thanks in advance for your help!
If the file is not on the server computer, then you will need to make sure SQL Server has access to the file. This becomes a permissions issue. In particular, the permission you need to look at is the log on account for the SQL Server service. Open the services control panel, locate the SQL Server service and check the log on account in the properties section.
As a general rule, it's not a good idea to give too many network permissions to the SQL Server service account because this can allow hackers access to resources outside the server computer.
If you mean the BULK INSERT command from T-SQL then the name of the 'data_file' can be a local filename or an UNC path, local that is to the SQL Server machine running the query. You can put the file to import from on a share hosted on the SQL Server machine or on any other share in the network the SQL Server has access to.