Solved

BULK INSERT format file with undelimited data

Posted on 2006-06-26
13
588 Views
Last Modified: 2009-05-07
Experts,

I need to import a flat txt-file with fixed-width columns into a database using BULK INSERT. Can I simply use the BCP format file to do this? What would the file look like?
0
Comment
Question by:HenningF
  • 7
  • 6
13 Comments
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 16983964
0
 
LVL 1

Author Comment

by:HenningF
ID: 16984021
This is basically an article from BOL which I allready read but from what I can see it doesn't cover the case where the data-file doesn't have any column delimiters...? Or is it me that just doesn't understand exactly how this thing works?
0
 
LVL 1

Author Comment

by:HenningF
ID: 16984063
here's a simple example file with the columns id, created and last_updated:
---------------------------------------------
000000154872006062020060623
000000154882006062020060625
000000154902006062021060622
---------------------------------------------

What would the format-file look like for this kind of data?
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 16984262
> I can see it doesn't cover the case where the data-file doesn't have any column delimiters...

From the description in the link "fuield terminator": "Null terminator (no visible terminator) \0 "

1 sqlchar 0 11 \0 1 id
2 sqlchar 0 08 \0 2 created
3 sqlchar 0 08 \0 3 last_updated
0
 
LVL 1

Author Comment

by:HenningF
ID: 16984534
Ok, great! I set up some testing here but I got an error message which doesn't make sense to me...do you have a clue? The database is SQL Server 2000 Std.

"Invalid data type for column number 1 in the format file "C:\Temp\bulk\bulk_format.txt"."

Format File:
---------------------------------------------
8.0
3
1      sqlchar      0      11      \0      1      id
2      sqlchar      0      08      \0      2      created
3      sqlchar      0      08      \0      3      last_updated
---------------------------------------------

Data file:
---------------------------------------------
000000154872006062020060623
000000154882006062020060625
000000154902006062021060622
---------------------------------------------

Database table
---------------------------------------------
CREATE TABLE [dbo].[bulktest](
      [ID] [char](11),
      [Created] [char](8),
      [Last_updated] [char](8)
)
---------------------------------------------

SQL statement:
---------------------------------------------
BULK INSERT testing.dbo.bulktest
FROM 'C:\Temp\bulk\bulk.txt' WITH (FORMATFILE='C:\Temp\bulk\bulk_format.txt');
---------------------------------------------
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 16984696
this has to do something with row terminator. If you merge your lines in the inpud file into one, it will work. Will try more.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 16984792
for the last row, specify terminator as \n

8.0
3
1     SQLCHAR     0     11     ""     1     id ""
2     SQLCHAR     0     08     ""     2     created ""
3     SQLCHAR     0     08     "\n"     3     last_updated ""
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 16984803
CORRECTION: "for the last row" -> "for the last FIELD"
0
 
LVL 1

Author Comment

by:HenningF
ID: 16984936
Hm, I don't get it... :

Server: Msg 4839, Level 16, State 1, Line 1
Cannot perform bulk insert. Invalid collation name for source column 3 in format file 'C:\Temp\bulk\bulk_format.txt'

I have tried adding the default database collation also which is Danish_Norwegian_CI_AS but without any luck.
0
 
LVL 1

Author Comment

by:HenningF
ID: 16985019
I gotta go home now...have been working 2 hours of overtime with this one but I'l check back in first thing tomorrow. Thank you very much for your help so far! :)
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 16985155
collation name is the empty string after the column name:

last_updated ""
0
 
LVL 1

Author Comment

by:HenningF
ID: 16990469
Ok, it seems I can't get this to work if my life depended on it! Making me furious because I belive this is simple but it just doesn't work. Have you actually tried the code at your end and made it work? I'm just curious because it sure doesn't work here...

This is what I got:

SQL:
BULK INSERT bulktest
FROM 'C:\Temp\bulk\bulk.txt' WITH (FORMATFILE='C:\Temp\bulk\bulk_format.txt');

Bulk.txt:
000000154872006062020060623
000000154882006062020060625
000000154902006062021060622

Bulk_format.txt:
8.0
3
1      SQLCHAR      0      11      ""      1      id      Danish_Norwegian_CI_AS
2      SQLCHAR      0      08      ""      2      created      Danish_Norwegian_CI_AS
3      SQLCHAR      0      08      "\r\n"      3      updated      Danish_Norwegian_CI_AS

I have tried setting version 7 and removing collation, terminators \r and \n together and by themselves, separating the settings in the format file with single-space and tab...is it possible that you do me the favor and try the code yourself? I'll double the points if you help me out on this one... :)
0
 
LVL 1

Author Comment

by:HenningF
ID: 16990586
AAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGHHHHHHHHHHHHHHHHH!!!

I just found the problem and it the most annoying thing ever!! I kept getting error messages no matter what I did and now, after several hours, for some reason I hit "enter" and put a blank line after the last column in the format file and that made the difference. To be honest it pisses me off...and it almost ruins the pleasure of finally making it work. But still; thanx alot for your help buddy...I couldn't have done it without you :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now