Link to home
Start Free TrialLog in
Avatar of VicZ
VicZFlag for United States of America

asked on

Cannot bulk load because the file could not be read. Operating system error code (null).

First create a test .CSV file ( c:\test.csv) add IP_adresses on one column and another column.

Example  test.CSV...

comp_name,IP_in,IP_out,speed
HP2003,11.215.25.100,95.145.35.5,fast
HP2004,11.215.25.101,95.145.35.5,slow
HP2005,11.215.25.102,95.145.35.5,fast

--create table in SQL (pubs, adventureworks, etc.)
CREATE TABLE CSV_data (
Track_No int IDENTITY(1,1),
Computer [char] (10),
IP_from [char] (15),
IP_to [char] (15),
Performance [char] (10)
)

Import to SQL using openrowset for CSV...

SELECT *
FROM    
OPENROWSET (
'MSDASQL',
'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=C:\',
'Select * from test.csv'
) AS Results

You will notice that the IP_Adress is incomplete.
I tried different field types (text, varchar, etc.)

A solution to this problem is BULK INSERT...

BULK INSERT CSV_Data
      FROM 'C:\test.csv'
      WITH
      (
         FIELDTERMINATOR =',',
         ROWTERMINATOR ='\n',
         FIRSTROW=1
      )

BULK INSERT makes it hard to insert new fields on the fly.


OPENROWSET has a BULK option that uses a fromat file.

SELECT *
FROM    
OPENROWSET (
BULK N'C:\test.csv',
FORMATFILE = 'c:\test.fmt')
SINGLE_BLOB
) AS Results

format file test.fmt content...
9.0
4
1       SQLCHAR       0       10      ","      1     comp_name        SQL_Latin1_General_Cp437_BIN
2       SQLCHAR       0       15      ","      2     IP_in            SQL_Latin1_General_Cp437_BIN
3       SQLCHAR       0       15      ","      3     IP_out           SQL_Latin1_General_Cp437_BIN
4       SQLCHAR       0       10      "\r\n"   4     speed            SQL_Latin1_General_Cp437_BIN

The problem now is that the OPENROWSET (BULK...) Option gives me the error...

Cannot bulk load because the file could not be read. Operating system error code (null).

You can try it with my notes above.
Avatar of Ram4020
Ram4020
Flag of Switzerland image

Does the file exists in db server or in your local. OPENROWSET looks for the file in db server.
Avatar of VicZ

ASKER

I am working on my localserver, I rean these...

EXEC master..xp_cmdshell 'dir c:\'
gives me my local drive contents.


exec xp_fileexist 'c:\test.csv'
File exists
Avatar of VicZ

ASKER

make sure you experts use the samples I put on the question, and also pay attention to the field names on the table, query, csv and format file. I have tried to optimize the format file as much as I could. I even tried tabs instead of commas, and I get the same error.
Avatar of VicZ

ASKER

I am rasing the points. I need this resolved.
Avatar of Mark Wills
Could it be the difference in rowterminators ?

the openrowset uses just \n but the format file uses \r\n
Avatar of VicZ

ASKER

Hi Mark, good to have you aboard.

No, I tried "\n" as a terminator too.

Did you recreate my problem on your computer?

I've been reading on this and there's a lot of feedback on account rights.

Windows Authentication vs SQL Server Authentication may be an issue too.

I am also stumped on the IP adress being cut off on the first OPENROWSET attempt.
Avatar of VicZ

ASKER

Just raised the points to 400.
Will try your files - would be better if you could attach one here... Any reason for that collation ?

It is approaching 6:00am down under and need some beauty sleep - will be back in a few hours though and will look then (if not answered already) - OK ?
Avatar of VicZ

ASKER

It's part iof the same project I've been working. Now the CSV files come in with different names and I need to do bulk inserts in to the table, plus I need to add more fields to along with that data, the bulk insert option doesn't provide that too well, openrowset does.

I know i has to do with permissions, almost sure about it.
Avatar of VicZ

ASKER

Has anyone replicated this problem yet?

My samples casn be cut and pasted.
No, cannot replicate... But, doing a copy and paste.... This works fine :

SELECT * FROM OPENROWSET (BULK N'c:\test.csv',FORMATFILE = 'c:\test.fmt',FIRSTROW=2) AS Results

No mention of single blob as you have in the above example (that will error anyway)

So, might be a permission problem, though the other versions did work, so maybe the format file cannot be read ?


Avatar of VicZ

ASKER

I am still getting the same error, I gave myself bulkadmin permissions too.

Are you logged in using Windows Authentication or SQL Server Authentication?
Windows authentication...

But it is unlikely that it is your data file because the other methods worked - well at least they read the file albeit incorrectly.
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VicZ

ASKER

You will not believe, what the difference between your FMT file and mine was.

Yours had a return line at the end of it. If you take that line off it fails on my system.

Crazy huh?

I guess just write down what just happened and I will accept it as a solution. Thanks again.
Well, it had to be the format file because the other methods read your data...

And yes, it can be a bit fussy. You could always use an xml format file as well...

Glad we were able to get to the bottom of it :)
Avatar of VicZ

ASKER

Thank you Mark.
Avatar of VicZ

ASKER

can you supply me a smaple of an XML format file for this solution so I can test it?
Sure...

Have a look at the attached - it is one method, there are some choices in style with XML format files, I find the attached easier to work with :)

Test1-format.xml
Avatar of VicZ

ASKER

Thank you Mark.  Unless I find an easy to use XML editor to do this, I guess I'll stick withthe text format file. It's already working for my project. Good work.
*laughing* Notepad works very well ;)