SQL Server Bulk Insert fails (Network related) - sql-server

I'm fairly new to SQL Server.
I'm trying to bulk insert into a table, using the command in SQL Server Management Studio (2005):
BULK INSERT Table1 FROM 'c:\text.txt' WITH (FIELDTERMINATOR = '|')
I get the error:
Msg 4860, Level 16, State 1, Line 1
Cannot bulk load. The file "c:\text.txt" does not exist.
I'm positive the file actually exists.
I get the feeling that it is looking for the file on the local hard drive for where ever the server is. Is that the case? If so, how do you generally solve this problem? (to note, I've tried specifying the network address of my PC when entering the location of the text file, but I get a permission error. Also, I know in advance that my company doesn't allow files to placed on a server).

SQL Server does not have an SQL statement that reads data from the client end (as the other posters have pointed out). Other RDBMS products do implement this (eg. the Postgres COPY statement lets you specify a file on the server or a file on the client that is read by the db connectivity library on the client side).
You can achieve moving data from a file on the client to a table on SQL Server using the bcp command line program.
bcp lets you copy data from a local file to a table on the server, or from a table (or select query) on the server into a local file. For example:
bcp servername.dbname.tablename in c:\temp.txt -T -c
will copy a tabbed delimited file (temp.txt) into the specified table (assuming the file contains the right number of columns).
I am not sure if this helps, but it is the only way to move data from a client file to a server table without giving the server some sort of access across the network to the data file on client.

I'd agree that it's a problem with the file being on your C drive, and not the server's drive.
If it's a permissions issue, have you tried creating a file share on your workstation that the server does have permissions to read from? Maybe something like \YourWorkstation\SQLFile, and then granting everyone (or Guest, depending on how your network permissions are set up) read access on it?
If you can't create the share on your laptop, or you can't grant rights to it for some reason, is there a file share somewhere in the office that you do have rights to, and that SQL can also read from? Maybe a NAS or a "Common" network folder?

Have you created a share drive on your machine that the server can see? If so then you just need to refer to the path including your machine name instead of C:

Yes, it will look for the file on the SQL server itself.
If you can map a network drive to the C drive of your SQL server, then you can just copy the file over before running the bulk insert.
If you absolutely can't get any access to the server's file system, then you can look at doing something like this:
write a program that reads your text file and inserts the contents into single record in a temporary table that has a Text field, perhaps using a stored procedure
have the program execute the bcp command to export the data from the temporary table into a text file on the SQL server's local file system, to a folder that the account under which the SQL service is running has write permission
have the program run a bulk insert command specifying the path to the text file on the server
delete the text file and the temporary table

Related

bulk insert returning error, "the file could not be opened"

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.

SQL Server Bulk Insert Format FIle could not be opened

I'm trying to use Bulk Insert to insert some data into my database on local computer. I am using a SQL Server Express database and executing query using Microsoft SQL Server Management Studio. When I try to execute a query I get this error.
Cannot bulk load because the file "‪D:\Countries.xml" could not be opened. Operating system error code 123(The filename, directory name, or volume label syntax is incorrect.).
I have been moving this file over my HDD by everywhere and still got the same error. To be honest I have no idea what is going on. Any tips?
BULK INSERT Research.dbo.Countries
FROM 'C:\Users\someuser\Desktop\Localization DB\countryInfo.txt' WITH
(
FORMATFILE='‪D:\Countries.xml',
FIRSTROW=2,
CHECK_CONSTRAINTS
);
GO
when you copy a path for example from properties of file
copied text have some extra thrash bytes, you need to view pasted text as ANSI and will be revealed
Are you 100% sure you are connecting to you locally installed SQL Server - not a networked one?
That's my favorite thing to do, rdp into a machine, or on a VM, and forget where I am physically connected. If you copy and paste from Word, sometimes it puts in the wrong type of ' back that out and replace.
I have no idea why but the problem was with ' mark being copied. When i deleted it and put it from keyboard it started working.

Bulk Insert - Does the file need to be accessible to database server or local machine?

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.

SQL Server Bulk Insert Http file

I am trying to bulk insert a csv file located on a remote web server but i am getting the following error.
Cannot bulk load because the file "http://34.34.32.34/test.csv" could
not be opened. Operating system error code 123(The filename, directory
name, or volume label syntax is incorrect.).
Is there anyway to accomplish this?
The documentation for BULK INSERT says nothing about SQL Server being able to connect to web servers.
http://msdn.microsoft.com/en-us/library/ms188365.aspx
' data_file ' Is the full path of the data file that contains data to
import into the specified table or view. BULK INSERT can import data
from a disk (including network, floppy disk, hard disk, and so on).
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.
If you must import a file from HTTP, consider writing a CLR stored procedure or using SSIS' external connectivity capabilities.
http://34.34.32.34/test.csv is, exactly as the error message says, an incorrect file name. Correct filenames look like c:\somefolder\test.csv. Something that starts with http: is an URL, not a file.
BULK INSERT does not support URLs as source. You should download the file first locally (using wget, curl or any other program that can download HTTP content), then bulk insert the downloaded file.

Insert a client file into a column on a server database

I wish to write a query that inserts a file that resides on the client (C# web server) into a column in the database server (SQL Server), something like INSERT … SELECT * FROM OPENROWSET(BULK…), but without having to save the file on the server machine first.
Is this even possible in SQL?
Although your context is unstated, I'm assuming that you're intending to run this from SSMS rather than from OSQL, a PowerShell script, or through some other means.
The file doesn't need to reside on the physical box running SQL Server, but SQL Server does need access to it. The typical approach, I believe, would be for an application server to copy the file to a shared repository and then pass it off to SQL Server through a UNC reference. The syntax to do so is relatively trivial and can be found in Importing Bulk Data by Using BULK INSERT or OPENROWSET(BULK...).
If instead you're interested in providing a mechanism for the SQL Server to save a file from some type of stream operation where the client is directly transmitting a file and there is no shared repository, I'm not aware of a way to do that. Even if you use an SQL FILESTREAM object you still need an accessible NTFS location to stream from. See Saving and Retrieving File Using FileStream SQL Server 2008.
At some point, the server will have to have a hand on the file. That does not mean that the server has to keep the file, but the file has to get to the server in order to be read and inserted into the db. Typically, this is achieved with a form and a file-type input. On the server, you can use the uploaded file to create your query, then delete it.
That said, storing files in a database is a debatable practice. Depending on the type and size of files you're storing, your database can quickly balloon in size. For starters, this makes backups slower and more prone to failure, along with a laundry list of other potential pitfalls. Check out this question on SO: Storing Images in DB - Yea or Nay? As you can see from the answers, there are a number of considerations to be made, but a good rule of thumb is to not do this unless you have compelling reasons to do so.
In SQL Server BLOB Data in .NET: Tutorial, Mohammad Elsheimy explains how it can be done:
using(SqlConnection con = new SqlConnection(conStr) )
using(SqlCommand command = new SqlCommand("INSERT INTO MyFiles VALUES (#Filename, #Data)", con) )
{
command.Parameters.AddWithValue("#Filename", Path.GetFileName(filename));
command.Parameters.AddWithValue("#Data", File.ReadAllBytes(filename));
connection.Open();
}
Basically, this way the file is read on the client and sent to the database server without a need for a temporary file on the server machine.

Resources