Solved

SQL Server Fixed Width File Import Issues

Posted on 2011-02-12
4
324 Views
Last Modified: 2012-05-11
Attached is a sample fixed width file of a flie I've downloaded from the FDA site. (I deleted most of the lines because it's huge.) This file, along with others, can be used to create a database of all the OTC & Rx drugs & the companies who registered them.

I haven't imported from a fixed width file before & I'm having problems.

Here is the structure for this file:

ListingSeqNum      Not Null      Num(7)      Col: 1-7      Primary Key
LblCode            Not Null      Char(6)      Col: 9-14
ProdCode      Not Null      Char(4)      Col: 16-19
Strength      Null      Char(10)      Col: 21-30
Unit      Null      Char(10)      Col: 32-41
Rx-OTC      Not Null      Char(1)      Col: 43
Tradename      Not Null      Char(100)      Col: 45-144

Here are my questions:

1. In the text file, there is a single space after every entry. Should I treat that space as a seperate column or as a field terminator?

2. When you look at a fixed width file, is there a way to determine what the field & row terminators are?  LISTINGS-X.TXT
0
Comment
Question by:LezlyPrime
[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
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34880150
1. You should treat the space as part of the preceding column. There are no field terminators on fixed file layouts.

Here is how your layout should be:

ListingSeqNum Not Null  Num(8)      Col: 1-8      Primary Key
LblCode       Not Null  Char(7)     Col: 9-15
ProdCode      Not Null  Char(5)     Col: 16-20
Strength      Null      Char(11)    Col: 21-31
Unit          Null      Char(11)    Col: 32-42
Rx-OTC        Not Null  Char(2)     Col: 44
Tradename     Not Null  Char(101)   Col: 45-144

Open in new window


2. Fixed files do not have field delimiters only row delimiters.  Generally row delimiters are the Carriage Return (0xD) and Line Feed (0xA) characters.

I hope this helps.
0
 

Author Comment

by:LezlyPrime
ID: 34909071
I followed your advice & extended each of my fields by one. Attached is the design of the Listings table.  395191443.jpg

When I run the following code, I get the following error:

CODE:

 
BULK
INSERT Listings
FROM 'C:\xfer\Rx\LISTINGS.TXT'
WITH
( ROWTERMINATOR = '\r\n'
)
GO

Open in new window

 

ERROR:

Msg 4866, Level 16, State 1, Line 1
The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified


Any help on what I'm doing wrong would be appreciated.

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 100 total points
ID: 34909569
You are missing the format file reference. Something like:

BULK
INSERT Listings
FROM 'C:\xfer\Rx\LISTINGS.TXT'
WITH
( FORMATFILE ='format_file_path'
)
GO

Open in new window

0
 

Author Comment

by:LezlyPrime
ID: 34911144
Thank you!

I didn't know about format files. I queried Experts Exchange & got more info on them & how to create them & got the data inserted.  
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 111
How do I fix this situation in GIT? 2 75
SQL works but want to get the XML node data separately 11 56
Notepad++ Question 11 26
This is an explanation of a simple data model to help parse a JSON feed
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

739 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