Solved

SQL Server Fixed Width File Import Issues

Posted on 2011-02-12
4
320 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 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

785 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