Solved

BULK INSERT format file with undelimited data

Posted on 2006-06-26
13
626 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

733 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