Solved

SQL Server Fixed Width File Import Issues

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql Audit table 3 48
Pivot Query Problem 9 31
SQL Server 2012 express 24 30
Following an example - removing duplicate strings 4 40
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This is about my first experience with programming Arduino.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now