Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

BULK INSERT format file with undelimited data

Posted on 2006-06-26
13
Medium Priority
?
651 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

610 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