Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server Fixed Width File Import Issues

Posted on 2011-02-12
4
Medium Priority
?
334 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
  • 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 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Screencast - Getting to Know the Pipeline

876 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